Skip to content
Sep 27 / jocke

Add timestamp to ping

I’ve been in some situations where I’d like to add timestamp to pings. There could be loads of reasons as to why you’d like to do this, but I used it to check if there where specific times of the day that had higher latency on my internet-connection at home.

If you’re only interested in pings that gets valid replies, the following should work on most systems;

ping google.com | awk '/^[0-9]+ bytes from / { "date" | getline pong; close("date"); print pong":",$0; }'

If you’re interested in all types of replies (unreachable, no route to host, etc), the following should work on most systems;

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

20 Comments

leave a comment
  1. Vikas / Jul 20 2011

    Thanks a lot.. Its indeed working fine..

  2. Joe Byrne / Oct 7 2011

    Can I get a timestamp similar to above by using -M mask | time and how do I use the -M, I can’t get it to work

  3. lutieri / Nov 15 2011

    very useful! nice jobs! thanks!

  4. Tung / Jan 5 2012

    gr8t, thks

  5. Mark V / Jan 6 2012

    Works great, cheers.

  6. MK2k / Mar 17 2012

    Thanks for this hint, works great!

  7. aaronforaudio / Apr 17 2012

    Thanks a lot!

  8. iandbige / Jul 26 2012

    thanks

  9. zpjet / Sep 17 2012

    Excellent. I used this one with a few ping switches to ping my ISP’s router and DNS to give them evidence that ca 1% of my pings get lost.

    Pinging every 10 seconds for 24 hours.

    ping -i 10 -c 8640 dns_ip_address | while read pong; do echo “$(date): $pong”; done > Desktop/dns_pings.txt

  10. Rob / Sep 25 2012

    cool. Thanks I knew there was a way… :-)

  11. Boris / Mar 21 2013

    thanks

  12. Gabe Rosas / Jun 16 2013

    ” ping google.com | while read pong; do echo “$(date): $pong”; done ”

    This is a line of pure genius. Thanks for sharing!!

  13. chris / Oct 4 2013

    roger that! genius

  14. egid / Feb 16 2014

    Great! Thanks a mil :)

  15. yurasuka / Jul 3 2014

    Genius indeed! Thanks!

  16. Doug / Feb 12 2015

    Still useful…..and eloquent

  17. tarunushka / May 26 2015

    Thanks a lot. But it does not show the ping statistics that I get when I do “ping google.com” something like “100 packets transmitted, 75 received, 25% packet loss, time 80015ms” etc. What to do to add ping statistics to above commands?

  18. jamie / Aug 26 2015

    I’m sorry, this is driving me crazy… PONG? Is that actually a real bash command, AND is it the response to ping? If so I’m lol, that’s cute :) But I can’t find use of it anywhere else… how did you discover this??

    • David / Sep 23 2015

      PONG is just a variable name. Nothing special at all, not a bash command.

  19. Victor Moran / Jan 27 2017

    Thanks for posting.
    (It took me a second to realize that pong was just a variable too.)

Leave a Comment