6

Ping can print you a timestamp on every line but its in unix date format :(

ping -D localhost  
PING localhost.localdomain (127.0.0.1) 56(84) bytes of data.
[1415629479.482938] 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.057 ms

I'm looking for a simple pipe command that can convert them on the fly to something like this:

[Sat 14 Feb 2009 01:31:30 SAST] 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.057 ms

Also I want it to run continuously, it shouldn't wait for the command to terminate before printing results.

3
  • UNIX time is easier to work with than structured time format Nov 10 '14 at 16:07
  • 1
    @HrvojeŠpoljar well I can't look at these unix times and know what time the pings stopped working, I'd prefer it in a human readable format
    – AmanicA
    Nov 10 '14 at 16:50
  • sure you can; take unix time and put it in command say... date -d '@timestamp' Nov 10 '14 at 16:51
4

From : https://stackoverflow.com/questions/14036116/convert-timestamp-to-datetime-with-sed

with bit of scripting...

ping -D localhost | while read row 
do 
  awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' <<< "$row"
done

Runs as

$ ping -D localhost | while read row; do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' <<< "$row" ; done
PING localhost (127.0.0.1) 56(84) bytes of data.
[2014-11-10 16:06:40.145811] 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.045 ms
[2014-11-10 16:06:41.144926] 64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.040 ms
5
  • I put that in a script and replaced localhost with $@; but it prints function strftime never defined with mawk and empty lines with gawk. What may I be missing?
    – Titus
    Jun 27 '17 at 15:37
  • this example works with gawk. I'm not sure what you intended to do by replacing localhost with $@ but if you pass multiple arguments it will likely break because ping command is expecting 1 argument for target host. Can you check if it's malfunctioning with original example which pings localhost? Jun 28 '17 at 10:51
  • Looks like it has been a c&p error. I typed it in and it works (with gawk), both with localhost and with $@. Btw I use $@ instead of $1 because I wanted to keep it more flexible: currently I add -i 15 before the address.
    – Titus
    Jun 28 '17 at 11:10
  • Oh one more thing: What might be the easiest way to get rid of the microseconds part and/or xx bytes from server.? str_replace in awk or cut?`
    – Titus
    Jun 28 '17 at 11:15
  • use this awk awk '{ sub(/[0-9]{10}\.[0-9]{5,8}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' to get rid of microseconds Jun 28 '17 at 13:38
2
ping google.com | awk '/^[0-9]+ bytes from / { "date" | getline pong; close("date"); print pong":",$0; }'

This comes from http://tech.jocke.no/2010/09/27/add-timestamp-to-ping/

It worked well also!

2

I think modifying the UNIX timestamp given by -D, as most answers suggest, is too complicated. Why not just make your own timestamp and stick it to the front of each line:

ping localhost | while read l; do echo `date` $l; done

You may give whatever formatting options to date, as you prefer.

1

On another server I was getting

awk: line 2: function strftime never defined

and there is no easy way to install gawk so I figured out how to do it without awk just with date (as per @Hrvoje Špoljar's hint)

ping -D  localhost | while read row; do 
    if [[ $row == \[*\]* ]]; then 
        echo -n \[$(date -d "@$(echo $row| sed 's/^\[//' | sed 's/\].*//')")\] ;
    fi ; 
    echo $row | sed 's/\[.*\]//' <<< "$row"; 
done

which yields

[Tue Apr 14 12:58:51 SAST 2015] 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=64 time=0.040 ms
1
  • That doesn´t seem to work with dash; throws a syntax error in line 2: exptected conditional binary operator, Syntax error at unexpected word >>\[*\]*<<. Do you have a hint?
    – Titus
    Jun 27 '17 at 15:46
1

This worked for me on macOS:

ping -i 5 google.com | while read pong; do echo "$(date -j '+%Y-%m-%d %H:%M:%S') $pong"; done

Adapted from the command given here: http://shawnreeves.net/wiki/index.php?title=Ping_with_timestamp_on_Mac_OS_X

The output looked like this:

2021-10-10 13:23:21 PING google.com (142.250.80.14): 56 data bytes
2021-10-10 13:23:21 64 bytes from 142.250.80.14: icmp_seq=0 ttl=115 time=83.045 ms
2021-10-10 13:23:26 64 bytes from 142.250.80.14: icmp_seq=1 ttl=115 time=39.765 ms
0

Above solution didn't work for me, I had to modify it a litte. Here is the version that works for me:

ping -D localhost | while read row; do awk '{ $1=strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10));print $0}' <<< $row;done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.