How to Enable Timestamp in Linux Ping Command Output

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:

Method 1)

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

Example:

$ ping www.linuxbuzz.com | while read pong; do echo "$(date): $pong"; done

Output of above command would be

Timestamp-ping-command-linux

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

Save-Ping-Command-Output-File

To Change the time format in ping’s reply, tweak date command parameters as shown below,

[[email protected] ~]$ 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 (104.27.152.86) 56(84) bytes of data.
2020-12-29_155507: 64 bytes from 104.27.152.86 (104.27.152.86): icmp_seq=1 ttl=59 time=53.0 ms
2020-12-29_155508: 64 bytes from 104.27.152.86 (104.27.152.86): icmp_seq=2 ttl=59 time=53.7 ms
2020-12-29_155509: 64 bytes from 104.27.152.86 (104.27.152.86): icmp_seq=3 ttl=59 time=52.6 ms
2020-12-29_155510: 64 bytes from 104.27.152.86 (104.27.152.86): 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
[[email protected] ~]$

Method 2)

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

ping-with-d-option-linux

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.

Also Read: How to Monitor Your Linux Servers Performance with Glances Tool

1 thought on “How to Enable Timestamp in Linux Ping Command Output”

Leave a Reply to paul Cancel reply

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

two × four =