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 |