Web page archived as of 2018-05-22. Some links & features might not work.

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.


Andreas Schamanek, 2013-08-02 11:27, 2013-08-02 11:28

It doesn't make much of a difference here, however, recent versions of Bash can do regex matches so we do not need sed or similar:

MYHOST=example.net ; while true ; do echo -n "$(date +%H:%M:%S) $MYHOST " ;\
[[ $(ping -q -c1 $MYHOST) =~ ^.*\ =\ ([0-9.]+).*ms ]] \
&& echo ${BASH_REMATCH[1]} ms || echo ; sleep 5 ; done
blog/080717_ping_ing_around.txt ยท Last modified: 2008-07-17 20:59 by andreas