Need Assistance on Ping Command

countryboy

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 76.234.111.182: 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 76.234.111.182: 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 76.234.111.182: icmp_seq=5 ttl=53 time=56.8 ms

--- 8.8.8.8 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!
 


TechnoJunky

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.
 

atanere

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
Code:
sudo apt install moreutils

Then give the ping command like this for 5 pings:
Code:
ping -c 5 google.com | ts

My output looks like this (the time is local computer time):
Code:
[email protected]:~$ ping -c 5 google.com | ts
Aug 07 20:33:55 PING google.com (172.217.4.46) 56(84) bytes of data.
Aug 07 20:33:55 64 bytes from ord38s18-in-f14.1e100.net (172.217.4.46): icmp_seq=1 ttl=54 time=13.1 ms
Aug 07 20:33:56 64 bytes from ord38s18-in-f14.1e100.net (172.217.4.46): icmp_seq=2 ttl=54 time=14.0 ms
Aug 07 20:33:57 64 bytes from ord38s18-in-f14.1e100.net (172.217.4.46): icmp_seq=3 ttl=54 time=11.7 ms
Aug 07 20:33:58 64 bytes from ord38s18-in-f14.1e100.net (172.217.4.46): icmp_seq=4 ttl=54 time=14.6 ms
Aug 07 20:33:59 64 bytes from ord38s18-in-f14.1e100.net (172.217.4.46): icmp_seq=5 ttl=54 time=10.6 ms
Aug 07 20:33:59
Aug 07 20:33:59 --- google.com 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.

Cheers
 

Members online


Top