Checking SSH failure in a script
Solution 1
You can check the return value that ssh gives you as originally shown here: How to create a bash script to check the SSH connection?
$ ssh -q user@downhost exit
$ echo $?
255
$ ssh -q user@uphost exit
$ echo $?
0
EDIT - I cheated and used nc
Something like this:
#!/bin/bash
ssh_port_is_open() { nc -z ${1:?hostname} 22 > /dev/null; }
for host in `cat /tmp/hosts` ; do
if ssh_port_is_open $host; then
ssh -o "BatchMode=yes" $i 'hostname; sudo ethtool eth1';
else
echo " $i Down"
fi
done
Solution 2
To check if there was a problem connecting and/or running the remote command:
if ! ssh host command
then
echo "SSH connection or remote command failed"
fi
To check if there was a problem connecting, regardless of success of the remote command (unless it happens to return status 255, which is rare):
if ssh host command; [ $? -eq 255 ]
then
echo "SSH connection failed"
fi
Applied to your example, this would be:
for i in `cat /tmp/hosts` ;
do
if ! ssh $i 'hostname;sudo ethtool eth1';
then
echo "Connection or remote command on $i failed";
fi
done
theuniverseisflat
Updated on June 14, 2022Comments
-
theuniverseisflat almost 2 years
Hi what is the best way to check to see if SSH fails for whatever reason? Can I use a IF statement ( if it fails then do something) I'm using the ssh command in a loop and passing my hosts names form a flat file.
so I do something like:
for i in `cat /tmp/hosts` ; do ssh $i 'hostname;sudo ethtool eth1'; done
I get sometime this error or I just cannot connect
ssh: host1 Temporary failure in name resolution
I want to skip the hosts that I cannot connect to is SSH fails. What is the best way to do this? Is there a runtime error I can trap to bypass the hosts that I cannot ssh into for whatever reason, perhaps ssh is not allowed or I do not have the right password ?
Thanking you in advance Cheers
-
theuniverseisflat about 10 yearsOk Thx wbt11a .. I know how to trap but in the context of the ssh command I have posted how would I do it? i need to use it within the same pass of this command for i in
cat /tmp/hosts
; do ssh $i 'hostname;sudo ethtool eth1'; done -
theuniverseisflat about 10 yearsOk Thx "ThatOtherGuy" .. I know how to trap but in the context of the ssh command I have posted how would I do it? i need to use it within the same pass of this command for i in
cat /tmp/hosts
; do ssh $i 'hostname;sudo ethtool eth1'; done -
theuniverseisflat about 10 yearsHi Thanks I got half way through. But I still get prompted 4 password hosts to which I do not have access to. How can I skip these hosts as well? Basically is there a way to escape the password challenged and jump to the next host?
-
theuniverseisflat about 10 yearsThanks I got half way through. But I still get prompted 4 password hosts to which I do not have access to. How can I skip these hosts as well? Basically is there a way to escape the password challenged and jump to the next host?
-
wbt11a about 10 yearsTry the '-o "BatchMode=yes"' flag seen in example.
-
theuniverseisflat about 10 yearsif [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
-
theuniverseisflat about 10 yearshi tried that and it's failing somewhere ? Can u help .. if [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
-
theuniverseisflat about 10 yearsif [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
-
wbt11a about 10 yearsssh -o "BatchMode=yes"
-
theuniverseisflat about 10 yearsI tried to put quote around the if statement inside the parantehsis . somehow the command is not bein interperted properly insde those brackets
-
wbt11a about 10 yearsIt was the person beneath me who was using the ! to test functionality.
-
theuniverseisflat about 10 yearsok thx .. ur answers are correct but not doing what I want .. I may have to open another question to see if I get some response back