Need Help ssh key fail on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help ssh key fail on remote server
# 1  
Old 09-01-2016
RedHat Need Help ssh key fail on remote server

Hello everyone,
I need some help writing a bash script to:
1. ssh from a jumpserver to 50 remote servers logging in automatically using ssh keys and capture if zabbix is running by running a "ps -ef | grep zabbix" and output to a logfile capturing the server hostname and ps -ef output to determine if zabbix is running.
2. I need the script to move on to the next server if ssh keys are not working and it asks for a password. It would be nice to have output to a log file all the servers where ssh keys are not working so I can go and fix the those servers later.

The problem I am having now is the script hangs on a server whenever the ssh keys dont work. So I really need some bash code for the script to skip to next server when this happens. I believe I need an if then statement script but cant figure out the exact syntax.

Here is what I have so far:
Code:
#!/bin/bash
for x in `cat server_list`;do echo $x>>zab_chk_output.txt; ssh $x ps -ef | grep zabbix>>zab_chk_output.txt
done

Appreciate everyone's help with this and thanks!
-vtowntechy
# 2  
Old 09-01-2016
Something like this...

Code:
for server in $(cat server_list);do
  {
  # set a different connect and command timeout ( you probably want to know the difference 
  # between an unreachable server and an unconfigured keybased authentication)
  output=$(timeout 10 ssh -o ConnectTimeout=8 $server ps -ef \| grep [z]abbix)
  code=$?
  if [ $code -ne 124 ]; then # return code 124 is the code when timeout has been reached
   echo "$server auth ok::$code::$output"
 else
   echo "$server auth failed" 
 fi
 } # Add a & before the # if you want to have all ssh connections started in parallel
done >zab_chk_output.txt
wait


Last edited by stomp; 09-02-2016 at 04:53 PM.. Reason: Removed unecessary { ... }
This User Gave Thanks to stomp For This Post:
# 3  
Old 09-02-2016
Some comments:
A timeout binary exists on some Linux platforms. There are also some timeout scripts available for download.
ConnectTimeout is a nice option on a recent openssh.
The [ ] needs to be escaped, otherwise the shell tries to find matches in the current directory (and would substitute it with a matching file name).
Because it is interpreted/substituted again on the remote host, it needs to be double-escaped.
Code:
 output=$(timeout 10 ssh -o ConnectTimeout=8 $server 'ps -ef | grep -w "[z]abbix"')

A word match grep -w is more precise.
Most precise would be pgrep -x that exists on Linux and some Unix platforms.
Code:
 output=$(timeout 10 ssh -o ConnectTimeout=8 $server 'pgrep -x zabbix')

--
The for-do-done is a block. So you don't need another { block } around it only to redirect its input or output.

Last edited by MadeInGermany; 09-02-2016 at 03:07 PM.. Reason: added missing )
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Remote Ssh Fail

Hi, I'm getting error when try to remote using ssh. I already check and ssh service is online. Does solaris 11 sparc need to do other setting. Error is interactive keyboard failed. Please assist me. Thanks. ---------- Post updated at 10:25 PM ---------- Previous update was at 08:49 PM... (0 Replies)
Discussion started by: mzainal
0 Replies

2. Shell Programming and Scripting

Multi server access through remote server using ssh

Team, Presently I have 5 ip address kept in ip_abc1 file, for each of the ip address listed, i need to login on each ipaddress one at a time and login as below for that specific ip address ssh -p 8101 karaf@<ip.address_for the specific ip address as logged in> password features:list... (4 Replies)
Discussion started by: whizkidash
4 Replies

3. IP Networking

Cygwin remote ssh with key authentication method

Hi experts, I am not sure in which forum to submit this question. If this is not the correct place then please let me know where to submit this thread. My requirement is to invoke windows batch scripts from linux shell script. Hence, I have installed openssh in Cygwin on the windows machine.... (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

4. Shell Programming and Scripting

Auto Remote SSH key setup

Hi Guys!! I am trying to get around the complex situation, i have a task to complete, Like to setup the remote SSH key automatically by providing the root login details, ip and ssh port once to the script input and once its tested and accepted the next ssh should be password less, script... (0 Replies)
Discussion started by: SilvesterJ
0 Replies

5. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

6. AIX

ssh public key auth "Remote login for account is not allowed" ?

Hello, Using AIX 6.1 boxes. User user1 connects from box A to box B using ssh. When password authentication is used everything is fine. When I configure user1 to use public key authentication sftp client works fine(no password asked), but ssh client fails. This is sshd log: Accepted publickey... (3 Replies)
Discussion started by: vilius
3 Replies

7. Shell Programming and Scripting

how to ssh to remote unix machines using private/public key

hello, iam able to ssh to a linux server from a linux server called "machine1" using the private/public key method, so I dont need to enter any password when I run my script but iam not able to ssh from machine1 to a UNIX server, access is denied. note that I am using an application id which is... (6 Replies)
Discussion started by: wydadi
6 Replies

8. Shell Programming and Scripting

Using ssh to add register key on ssh server

Hi, I want to use ssh to add a register key on remote ssh server. Since there are space characters in my register key string, it always failed. If there is no space characters in the string, it worked fine. The following is what I have tried. It seems that "ssh" command doesn't care about double... (9 Replies)
Discussion started by: leaftree
9 Replies

9. Shell Programming and Scripting

Generate Public Key when the server is not ssh enabled

I am writing a script that needs to access various servers some of which are not ssh enabled. In order to access the ssh enabled servers I am using the following command to generate the public key : ssh-keygen -t rsa Is there a similar command for the other servers as well. If I try to use... (1 Reply)
Discussion started by: ravneet123
1 Replies

10. Cybersecurity

SSH key code versus server key code

Hi, When logging in using SSH access (to a remotely hosted account), I received a prompt to accept a server's key fingerprint. Wrote that string of code down for comparision. Already emailed my host for their listing of the string of code for the server's key fingerprint (for comparison,... (1 Reply)
Discussion started by: Texan
1 Replies
Login or Register to Ask a Question