how to use ping in a script
Use the -w
switch (or -t
on FreeBSD and OS X) on the ping
command, then inspect the command's return value.
ping -w 1 $cRETVAL=$?if [ $RETVAL -eq 0 ]; then ssh $c 'check something'fi
You may want to adjust the parameter you pass with -w
if the hosts you're connecting to are far away and the latency is higher.
From man ping
:
-w deadline Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.