Need Assistance on Ping Command


New Member
Hi All,

I've been googling all day and I can't really seem to find what I am looking for.

I am wanting a ping command that will display the time, and if the ping was successful or failed.

Something like the following

[Wed Jul 31 02:01:27 EDT 2019] 64 bytes from icmp_seq=1 ttl=53 time=53.2 ms
[Wed Jul 31 02:01:28 EDT 2019] no answer yet for icmp_seq=2
[Wed Jul 31 02:01:29 EDT 2019] 64 bytes from icmp_seq=3 ttl=53 time=56.4 ms
[Wed Jul 31 02:01:30 EDT 2019] no answer yet for icmp_seq=4
[Wed Jul 31 02:01:31 EDT 2019] 64 bytes from icmp_seq=5 ttl=53 time=56.8 ms

--- ping statistics ---
5 packets transmitted, 3 received, 40% packet loss

I don't have access to my linux machine yet, but if someone could give me a starting point on doing something like this, it would be great!


Well-Known Member
from what I can tell, Ping doesn't have this option. So you may need to script it. so the script would print the time and the result of 1 ping, and then loop if you want.


Well-Known Member
Ping does have a timestamp option (-D)... that outputs in Unix time. There are converters, but that isn't a good method.

If you install the moreutils package, you will get a new command, ts (timestamp). This will give a clean time and date stamp that you could work with. You may have some ability to configure how the date/time looks, but you may not be able to get your format exactly.

Ping normally runs continuously until you stop it with CTRL-C, but the command also allows you to specify how many pings you want to send with the -c option.

So, if you will be using a Debian/Ubuntu based distro, first install moreutils
sudo apt install moreutils

Then give the ping command like this for 5 pings:
ping -c 5 | ts

My output looks like this (the time is local computer time):
[email protected]:~$ ping -c 5 | ts
Aug 07 20:33:55 PING ( 56(84) bytes of data.
Aug 07 20:33:55 64 bytes from ( icmp_seq=1 ttl=54 time=13.1 ms
Aug 07 20:33:56 64 bytes from ( icmp_seq=2 ttl=54 time=14.0 ms
Aug 07 20:33:57 64 bytes from ( icmp_seq=3 ttl=54 time=11.7 ms
Aug 07 20:33:58 64 bytes from ( icmp_seq=4 ttl=54 time=14.6 ms
Aug 07 20:33:59 64 bytes from ( icmp_seq=5 ttl=54 time=10.6 ms
Aug 07 20:33:59
Aug 07 20:33:59 --- ping statistics ---
Aug 07 20:33:59 5 packets transmitted, 5 received, 0% packet loss, time 4004ms
Aug 07 20:33:59 rtt min/avg/max/mdev = 10.683/12.841/14.671/1.472 ms

Hopefully that is enough to get you started. I would probably find a way to evaluate the "% packet loss" value to determine success. Maybe just use a single ping... pass or fail.


Members online