On occasion I’ve been to clients where ICMP is turned off
at the routers. This means servers will not respond to a ping if
you wanted to determine if a server is alive. The following script
can be used to test an ssh connection instead and timeout within a
reasonable time. In this case 7 seconds.

user=troy
do_ssh()
{
  ssh $user@$1 $2 &
  PID=$!
  (sleep 7; kill $PID >/dev/null 2>&1) &
  wait $PID >/dev/null 2>&1
}
 
do_ssh $host 'date' >/dev/null 2>&1
if [[ $? -gt 0 ]];then echo
  "${host} : SSH Timeout"
  exit 1
else
  # SSH is ok, so run your command!
  ssh $host 'ps -ef'
fi

About troyski

I'm a freelance UNIX engineer working in the UK. I'm married to Tina and between us we have six children. I'm a bit of an Apple fan boy, and all the Windows machines in the house are a thing of the past now.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Private

Post navigation