When I ping I have this display:
> ping -i 4 www.google.fr
64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=503 ttl=46 time=45.5 ms
.......
.......
64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=508 ttl=46 time=44.9 ms
64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=509 ttl=46 time=45.1 ms
I'd like to have the time of the ping before.
Something like:
> (right functions) + ping -i 7 www.google.fr
mardi 15 mai 2012, 10:29:06 (UTC+0200) - 64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=503 ttl=46 time=45.5 ms
.......
.......
mardi 15 mai 2012, 10:29:13 (UTC+0200) - 64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=508 ttl=46 time=44.9 ms
mardi 15 mai 2012, 10:29:20 (UTC+0200) - 64 bytes from wi-in-f94.1e100.net (173.194.67.94): icmp_seq=509 ttl=46 time=45.1 ms
How would you do this in a command line (if it's possible)?
Use:
You will get the result like this:
1. time from
ping -D
Another possibility to use the
ping -D
option which gets you the timestamp as Unix time.here is a
awk
cmd to parse timestamp to date format:PS: awk may full-buffered with pipe, a
fflush()
afterprint
will fix:2. time from
date
Here a version of "Achu" command with slightly different format:
That gets you:
There is a utility called
ts
, which reads stdin, adds timestamps, and writes it to stdout:It can be installed in Ubuntu with
sudo apt install moreutils
.You can also use
gawk
(orawk
, if your/etc/alternatives/awk
points to/usr/bin/gawk
):This is similar to the approach in Achu's answer, but
ping
's output is piped togawk
instead of a shell loop that callsdate
. As with that approach, it works without-c
, but if you don't pass-c n
to make ping stop after n pings, and you stop the loop with Ctrl+C,ping
won't print the usual statistics.This happens whether
ping
's output is piped togawk
or a shellwhile
loop. The reason is that command on the right side of the pipe, rather thanping
, receives SIGINT when Ctrl+C is pressed, andping
does not know to print the statistics before being terminated.If you have run
ping
without-c
on the left side of a pipe (as shown above) and you want to terminate it in such a way that it still prints the statistics, then instead of pressing Ctrl+C in the terminal where it is running, you could runkill -INT PID
from another terminal, replacingPID
with the process ID of theping
command. If you're only running one instance ofping
then you could simply usekillall -INT ping
.Alternatively, you could replace the
ping
command on the left side of the pipe with a command that runs a shell, reports the process ID of that shell, and then replaces that shell with theping
command (causing it to have the same PID):Then the first line of output, will show the process ID of the
ping
command (which will typically be different each time). It would look like this, but with a different time and date and probably a different process ID:Then, from another terminal, you can run
kill -INT 7557
, replacing7557
with the actual process ID you saw, to terminate theping
command in such a way as to cause it to print statistics.(If you take advantage of your shell's job control features, then you can achieve this within the same terminal, too. But if you want to copy text from your terminal without having to remove any extranous part where you ran commands in that terminal, then you should terminate
ping
from a separate terminal.)Further reading:
man ping
- "When the specified number of packets have been sent (and received) or if the program is terminated with a SIGINT, a brief summary is displayed."If your interested in saving it in file, then type the below command in the terminal
You don't need to create any text files, it'll automatically do
Ping_Test.txt
(thanks to Achu and Eliah Kagan for the ideas) there's a way to
ping
outputping
ctrl+c
to do this one should instruct right part of the command (after the pipe) to ignore
SIGINT
usingtrap "" INT
: