Timestamp Ping & Save Log to the output file in Linux.

By | April 10, 2022

The ping command is one of the important network troubleshooting tools to test
reachability between the source and destination. Timestamp ping is a great tool
for troubleshooting the network because It will give you the ping report along
with the time frame so that you can identify what was the issue at that particular
time frame.

in this post, we will discuss, How we can ping to a destination with having a timestamp
in Linux.

There are many ways to print the timestamp of each ping reply in Linux. But I will talk about
one of my favorite commands which I use on daily basis. Find the command given below.

ping <Host-IP/Domain-Name> | while read pong; do echo “$(date): $pong”; done

For Example

[root@localhost ~]#ping www.google.com | while read pong; do echo "$(date): $pong"; done

[root@localhost ~]#ping 8.8.8.8 | while read pong; do echo "$(date): $pong"; done

Now lets Ping with the given command and see the output given below.

1-Ping With Time Stamp in Linux.

Let’s ping google.com, and we will see the ping reports with time stamps. The output is given below.

[root@localhost ~]# ping www.google.com | while read pong; do echo "$(date): $pong"; done
Mon Apr 11 00:42:43 IST 2022: PING www.google.com (172.217.160.132) 56(84) bytes of data.
Mon Apr 11 00:42:43 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=1 ttl=121 time=39.3 ms
Mon Apr 11 00:42:44 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=2 ttl=121 time=38.5 ms
Mon Apr 11 00:42:45 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=3 ttl=121 time=38.7 ms
Mon Apr 11 00:42:46 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=4 ttl=121 time=38.6 ms
Mon Apr 11 00:42:47 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=5 ttl=121 time=38.6 ms
Mon Apr 11 00:42:48 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=6 ttl=121 time=38.7 ms
Mon Apr 11 00:42:49 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=7 ttl=121 time=38.5 ms
Mon Apr 11 00:42:50 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=8 ttl=121 time=38.6 ms
Mon Apr 11 00:42:51 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=9 ttl=121 time=38.6 ms
Mon Apr 11 00:42:52 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=10 ttl=121 time=38.5 ms
Mon Apr 11 00:42:53 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=11 ttl=121 time=38.6 ms
Mon Apr 11 00:42:54 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=12 ttl=121 time=38.6 ms
Mon Apr 11 00:42:55 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=13 ttl=121 time=38.6 ms
^C
[root@localhost ~]#

2–Ping With Timestamp and Save your log output to a file.

Now let’s ping google.com with the timestamp and we will save that ping result in a text file on our tmp file (/tmp/ping.log). The command I have given below.

ping www.google.com | while read pong; do echo "$(date): $pong"; done > /tmp/ping.log

Now, let’s run the command to save our ping report.

[root@localhost ~]# ping  www.google.com | while read pong; do echo "$(date): $pong"; done > /tmp/ping.log    
^C
 
[root@localhost ~]# cat /tmp/ping.log 
Mon Apr 11 01:03:42 IST 2022: PING www.google.com (172.217.160.132) 56(84) bytes of data.
Mon Apr 11 01:03:42 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=1 ttl=121 time=39.1 ms
Mon Apr 11 01:03:43 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=2 ttl=121 time=38.5 ms
Mon Apr 11 01:03:44 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=3 ttl=121 time=38.5 ms
Mon Apr 11 01:03:45 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=4 ttl=121 time=38.5 ms
Mon Apr 11 01:03:46 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=5 ttl=121 time=39.0 ms
Mon Apr 11 01:03:47 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=6 ttl=121 time=38.5 ms
Mon Apr 11 01:03:48 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=7 ttl=121 time=38.6 ms
Mon Apr 11 01:03:49 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=8 ttl=121 time=38.5 ms
Mon Apr 11 01:03:50 IST 2022: 64 bytes from maa03s29-in-f4.1e100.net (172.217.160.132): icmp_seq=9 ttl=121 time=38.6 ms
[root@localhost ~]# 
[root@localhost ~]#

Leave a Reply

Your email address will not be published.