Skip to content

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

26 Comments

  1. Vikas Vikas

    Thanks a lot.. Its indeed working fine..

  2. 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 lutieri

    very useful! nice jobs! thanks!

  4. Tung Tung

    gr8t, thks

  5. Mark V Mark V

    Works great, cheers.

  6. Thanks for this hint, works great!

  7. aaronforaudio aaronforaudio

    Thanks a lot!

  8. iandbige iandbige

    thanks

  9. zpjet zpjet

    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 Rob

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

  11. Boris Boris

    thanks

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

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

  13. chris chris

    roger that! genius

  14. egid egid

    Great! Thanks a mil :)

  15. yurasuka yurasuka

    Genius indeed! Thanks!

  16. Doug Doug

    Still useful…..and eloquent

  17. tarunushka tarunushka

    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 jamie

    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 David

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

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

  20. dag dag

    Super helpful, thanks!

  21. Atomo Atomo

    I tried this and got this written in blue. I am lost. Any help would be much appreciated.

    ~ pipe while quotes

  22. J$ J$

    terminal didn’t like the quotes… (os x 10.14.5)
    After removing them i’ve found my new favorite command :D

    ping http://www.apple.com | while read pong; do echo $(date): $pong; done

  23. Paul P Paul P

    Love this! So handy and elegant. Piping to a file gives me a record if I need it.

  24. James C Brantley James C Brantley

    Awesome!

  25. Alex D Alex D

    Great, thanks for sharing!

Leave a Reply to chris Cancel reply

Your email address will not be published. Required fields are marked *