How do I timestamp every ping result?

John Mee picture John Mee · May 21, 2012 · Viewed 129.6k times · Source

Ping returns this by default:

64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

Is there some way I can get it to add the timestamp?

For example,

Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

I'm on OS X v10.7 (Lion) which seems to have some BSD version of ping.

Answer

richk picture richk · Jun 26, 2013

I could not redirect the Perl based solution to a file for some reason so I kept searching and found a bash only way to do this:

ping www.google.fr | while read pong; do echo "$(date): $pong"; done

Wed Jun 26 13:09:23 CEST 2013: PING www.google.fr (173.194.40.56) 56(84) bytes of data.
Wed Jun 26 13:09:23 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=1 ttl=57 time=7.26 ms
Wed Jun 26 13:09:24 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=2 ttl=57 time=8.14 ms

The credit goes to https://askubuntu.com/a/137246