Ping is the most widely used command on Linux and Unix servers for network troubleshooting. With the ping command we generally check connectivity to remote system either via its ip address or dns name. There can be some situations where we want to capture time in each ping reply. In this article, I will demonstrate how to enable timestamp in ping command output.
There are different ways to capture timestamp of each ping reply. Some of them are listed below:
Run the following command from terminal to enable timestamp in ping reply / output.
$ ping <Remote-Host-IP/DNS-Name> | while read pong; do echo “$(date): $pong”; done
$ ping www.linuxbuzz.com | while read pong; do echo "$(date): $pong"; done
Output of above command would be
If you wish to save above command output to a file then run,
$ ping -c 4 www.linuxbuzz.com | while read pong; do echo "$(date): $pong"; done > /tmp/TimeStamp-Ping.log
To Change the time format in ping’s reply, tweak date command parameters as shown below,
[pkumar@linuxbuzz ~]$ ping -c 4 www.linuxbuzz.com | while read pong; do echo "$(date +%Y-%m-%d_%H%M%S): $pong"; done 2020-12-29_155507: PING www.linuxbuzz.com (220.127.116.11) 56(84) bytes of data. 2020-12-29_155507: 64 bytes from 18.104.22.168 (22.214.171.124): icmp_seq=1 ttl=59 time=53.0 ms 2020-12-29_155508: 64 bytes from 126.96.36.199 (188.8.131.52): icmp_seq=2 ttl=59 time=53.7 ms 2020-12-29_155509: 64 bytes from 184.108.40.206 (220.127.116.11): icmp_seq=3 ttl=59 time=52.6 ms 2020-12-29_155510: 64 bytes from 18.104.22.168 (22.214.171.124): icmp_seq=4 ttl=59 time=52.3 ms 2020-12-29_155510: 2020-12-29_155510: --- www.linuxbuzz.com ping statistics --- 2020-12-29_155510: 4 packets transmitted, 4 received, 0% packet loss, time 3002ms 2020-12-29_155510: rtt min/avg/max/mdev = 52.336/52.959/53.769/0.534 ms [pkumar@linuxbuzz ~]$
By Passing ‘-D’ option in ping command, we can enable timestamp in the output. Example is shown below.
$ ping -D -c 4 www.linuxbuzz.com
In the above ping command output, Unix time plus microseconds are printed in-front of each ping reply.
If you wish to create a shell script that will capture ping reply along with timestamp, then create a file with below contents.
#!/bin/bash #Script will continuously ping NFS VIP and capture timestamp DIR=/var/tmp/nfs-ping-pong if [ -d $DIR ] then echo "Directory Exists" else mkdir -p $DIR fi ping 10.8.3.102 | while read pong; do echo "$(date): $pong" >> $DIR/nfs-ping-$(date +%Y-%m-%d).txt; done
In the above script, you can replace remote NFS server IP address that suits to your setup. You can also schedule this script as a cron job. That’s all from this article, please do share your feedback and comments.