Home Linux Print Timestamp With Ping Command Output In Linux
Print Timestamp With Ping Command Output In Linux

Print Timestamp With Ping Command Output In Linux

By sk
1346 Views

Ping is a commandline network administration utility which is available for virtually all operating systems, including the embedded networking devices. It is mainly used to test the connectivity or reachability of a host on the network. By default, the ping command will not display the timestamp in its output. If you want to print timestamp with ping command output for any reason, this tutorial will show you how.

Print timestamp with Ping command output in Linux

To display timestamp in ping command's output, simply pass the -D flag like below.

$ ping -c 3 -D ostechnix.com

Sample output:

PING ostechnix.com(2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886)) 56 data bytes
[1611825018.797092] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=1 ttl=57 time=94.8 ms
[1611825019.616530] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=2 ttl=57 time=114 ms
[1611825020.615700] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=3 ttl=57 time=112 ms
[1611825021.596463] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=4 ttl=57 time=91.5 ms
[1611825022.619467] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=5 ttl=57 time=113 ms

--- ostechnix.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 91.543/105.095/114.408/9.815 ms
Print Timestamp With Ping Command Output
Print Timestamp With Ping Command Output

As you see in the above output, the ping command printed the timestamp (unix time + microseconds as in gettimeofday) before each line. In my case, [1611825018.797092], [1611825019.616530], .... [1611825022.619467] are the timestamps.

Even though Ping command has the option to enable timestamp, the output is not so user-friendly. If you want to display the timestamp in human-readable format, use ts command as shown in the following section.

Enable timestamp in Ping command output with ts command

The ts command is part of moreutils package. It prints the timestamp to the beginning of each line in any Linux command's output.

To use ts command, you need to install moreutlis package as shown in the following link:

Now, you can show the timestamp in each line in the output of ping command like below:

$ ping -c 5 ostechnix.com | ts

Sample output:

Jan 28 16:38:40 PING ostechnix.com(2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886)) 56 data bytes
Jan 28 16:38:40 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=1 ttl=57 time=76.7 ms
Jan 28 16:38:41 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=2 ttl=57 time=93.2 ms
Jan 28 16:38:42 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=3 ttl=57 time=84.8 ms
Jan 28 16:38:43 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=4 ttl=57 time=84.3 ms
Jan 28 16:38:44 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=5 ttl=57 time=108 ms
Jan 28 16:38:44 
Jan 28 16:38:44 --- ostechnix.com ping statistics ---
Jan 28 16:38:44 5 packets transmitted, 5 received, 0% packet loss, time 4006ms
Jan 28 16:38:44 rtt min/avg/max/mdev = 76.718/89.390/107.965/10.646 ms
Enable timestamp in Ping command output with ts command
Enable timestamp in Ping command output with ts command

Did you notice the timestamp at the beginning of each line? Yes, they are the timestamps and the are easy to understand now. Not just for ping command, the ts command can display the timestamp for any command's output.

Hope this helps.

Related read:

You May Also Like

1 comment

Jalal January 29, 2021 - 11:23 am

Hi,
Thank you so much for the great topic,

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More