Checking SSH failure in a script

18,754

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
Share:
18,754
theuniverseisflat
Author by

theuniverseisflat

Updated on June 14, 2022

Comments

  • theuniverseisflat
    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
    theuniverseisflat about 10 years
    Ok 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
    theuniverseisflat about 10 years
    Ok 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
    theuniverseisflat about 10 years
    Hi 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
    theuniverseisflat about 10 years
    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?
  • wbt11a
    wbt11a about 10 years
    Try the '-o "BatchMode=yes"' flag seen in example.
  • theuniverseisflat
    theuniverseisflat about 10 years
    if [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
  • theuniverseisflat
    theuniverseisflat about 10 years
    hi tried that and it's failing somewhere ? Can u help .. if [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
  • theuniverseisflat
    theuniverseisflat about 10 years
    if [ ! ssh -o $1 ] ; then echo sshfailed else echo challenging me still fi
  • wbt11a
    wbt11a about 10 years
    ssh -o "BatchMode=yes"
  • theuniverseisflat
    theuniverseisflat about 10 years
    I tried to put quote around the if statement inside the parantehsis . somehow the command is not bein interperted properly insde those brackets
  • wbt11a
    wbt11a about 10 years
    It was the person beneath me who was using the ! to test functionality.
  • theuniverseisflat
    theuniverseisflat about 10 years
    ok 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