Ping'ing around

If I keep a ping running I sometimes find it difficult to see if a host is still responding, especially when RTTs are more or less constant and all that is changing from one line to the other is the sequence counter. Here is a one-liner to combine ping with date:

while true ; do echo "$(date) google.com $(ping -q -c1 google.com | sed -ne '$s/^.* = //p' )" ; sleep 4 ; done

Until one presses Ctrl-C this will ask ping to send 1 ping (-c1) and be quiet (-q) about it. ping still produces the statistics, though. sed takes their last ($) line, removes everything up until the = sign and prints it out (p).

A little more style and comfort can be done for instance with

MYHOST=google.com ; while true ; do echo "$(date +%H:%M:%S) $MYHOST $(ping -q -c1 $MYHOST \
| sed -ne '$s/^.* = \([0-9.]\+\).*ms/\1 ms/p' )" ; sleep 5 ; done

This should produce something similar to

20:52:19 google.com 124.062 ms
20:52:24 google.com 124.062 ms
20:52:29 google.com 116.058 ms

If you have a computer which is currently not responding to ping and one wants to know when it is one could simply use audible pings (-a) like in

ping -a hostname_or_ip-address

If you want to put this into a script which is called by cron you might want to have a look at the example provided with my Bash script postpone.