如何命令“Ping”显示ping的时间和日期

当我ping我有这个显示:

> 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 

我想先得到ping的时间。

就像是:

 > (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 

你会如何在命令行中执行此操作(如果可能的话)?

使用:

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

你会得到这样的结果:

在此处输入图像描述

使用ping -D选项的另一种可能性是将时间戳作为Unix时间。

 tilo@t-ubuntu:~$ ping google.com -D PING google.com (173.194.33.73) 56(84) bytes of data. [1388886989.442413] 64 bytes from sea09s15-in-f9.1e100.net (173.194.33.73): icmp_req=1 ttl=57 time=11.1 ms [1388886990.443845] 64 bytes from sea09s15-in-f9.1e100.net (173.194.33.73): icmp_req=2 ttl=57 time=11.0 ms [1388886991.445200] 64 bytes from sea09s15-in-f9.1e100.net (173.194.33.73): icmp_req=3 ttl=57 time=10.8 ms [1388886992.446617] 64 bytes from sea09s15-in-f9.1e100.net (173.194.33.73): icmp_req=4 ttl=57 time=10.9 ms ^C --- google.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 10.860/11.005/11.139/0.123 ms tilo@t-ubuntu:~$ 

这里有一个版本的“Achu”命令,格式略有不同:

 ping www.google.com -i 10 -c 3000 | while read pong; do echo "$(date +%Y-%m-%d_%H%M%S): $pong"; done >PingTest_2014-01-04.log 

这会让你:

 2014-01-04_175748: 64 bytes from sea09s16-in-f19.1e100.net (173.194.33.115): icmp_req=13 ttl=57 time=10.5 ms 

你也可以使用gawk (或awk ,如果你的/etc/alternatives/awk指向/usr/bin/gawk ):

 ping -c 4 www.google.fr | gawk '{print strftime("%c: ") $0}' 

这类似于Achu的答案中的方法,但ping的输出通过管道传输给gawk而不是调用date的shell循环。 与该方法一样,它在没有-c情况下工作,但是如果你没有通过-c n来使n ping之后停止ping,并且你用Ctrl + C停止循环,则ping将不会打印通常的统计信息。

 ek@Io:~$ ping -c 4 www.google.fr | gawk '{print strftime("%c: ") $0}' Tue 03 Jan 2017 10:09:51 AM EST: PING www.google.fr (216.58.193.99) 56(84) bytes of data. Tue 03 Jan 2017 10:09:51 AM EST: 64 bytes from sea15s08-in-f3.1e100.net (216.58.193.99): icmp_seq=1 ttl=51 time=327 ms Tue 03 Jan 2017 10:09:52 AM EST: 64 bytes from sea15s08-in-f3.1e100.net (216.58.193.99): icmp_seq=2 ttl=51 time=302 ms Tue 03 Jan 2017 10:09:53 AM EST: 64 bytes from sea15s08-in-f3.1e100.net (216.58.193.99): icmp_seq=3 ttl=51 time=282 ms Tue 03 Jan 2017 10:09:54 AM EST: 64 bytes from sea15s08-in-f3.1e100.net (216.58.193.99): icmp_seq=4 ttl=51 time=349 ms Tue 03 Jan 2017 10:09:54 AM EST: Tue 03 Jan 2017 10:09:54 AM EST: --- www.google.fr ping statistics --- Tue 03 Jan 2017 10:09:54 AM EST: 4 packets transmitted, 4 received, 0% packet loss, time 3003ms Tue 03 Jan 2017 10:09:54 AM EST: rtt min/avg/max/mdev = 282.035/315.227/349.166/25.398 ms 
 ek@Io:~$ ping www.google.fr | gawk '{print strftime("%c: ") $0}' Tue 03 Jan 2017 10:10:35 AM EST: PING www.google.fr (216.58.193.99) 56(84) bytes of data. Tue 03 Jan 2017 10:10:35 AM EST: 64 bytes from sea15s08-in-f99.1e100.net (216.58.193.99): icmp_seq=1 ttl=51 time=305 ms Tue 03 Jan 2017 10:10:35 AM EST: 64 bytes from sea15s08-in-f99.1e100.net (216.58.193.99): icmp_seq=2 ttl=51 time=365 ms Tue 03 Jan 2017 10:10:36 AM EST: 64 bytes from sea15s08-in-f99.1e100.net (216.58.193.99): icmp_seq=3 ttl=51 time=390 ms Tue 03 Jan 2017 10:10:38 AM EST: 64 bytes from sea15s08-in-f99.1e100.net (216.58.193.99): icmp_seq=4 ttl=51 time=824 ms Tue 03 Jan 2017 10:10:38 AM EST: 64 bytes from sea15s08-in-f99.1e100.net (216.58.193.99): icmp_seq=5 ttl=51 time=287 ms ^C 

无论ping的输出是通过管道传输到gawk还是shell的while循环都会发生这种情况。 原因是当按下Ctrl + C时,管道右侧的命令(而不是ping )接收SIGINT ,并且ping不知道在终止之前打印统计信息。

如果你在管道的左侧运行没有-c ping (如上所示),并且你想以它仍然打印统计信息的方式终止它,那么不要在终端中按Ctrl + C而不是运行时,可以从另一个终端运行kill -INT PID ,用ping命令的进程ID替换PID 。 如果你只运行一个ping实例,那么你可以简单地使用killall -INT ping

或者,您可以使用运行shell 的命令替换管道左侧的ping命令,报告该shell的进程ID,然后使用ping命令替换该shell(使其具有相同的PID):

 sh -c 'echo $$; exec ping www.google.fr' | gawk '{print strftime("%c: ") $0}' 

然后第一行输出将显示ping命令的进程ID(每次通常不同)。 它看起来像这样,但具有不同的时间和日期,可能还有不同的进程ID:

 Tue 20 Mar 2018 12:11:13 PM EDT: 7557 

然后,从另一个终端,您可以运行kill -INT 7557 ,用您看到的实际进程ID替换7557 ,以终止ping命令,使其打印统计信息。

(如果您利用shell的作业控制function,那么您也可以在同一个终端中实现此function。但是如果您想从终端复制文本而不必删除在该终端中运行命令的任何超常部分,那么你应该从一个单独的终端终止ping 。)

进一步阅读:

  • Alin回答了如何导出当前日期和时间,并在Stack Overflow上包含’Hello’的每一行的末尾附加 。
  • 9.1.5 Gawk中的 时间函数 :有效的AWK编程 (Gawk的官方参考手册)。
  • man ping – “ 当发送(和接收)指定的数据包数量或者程序以SIGINT终止时,将显示一个简短的摘要。
 ping google.in | xargs -n1 -i bash -c 'echo `date +"%Y-%m-%d %H:%M:%S"`" {}"' 

如果您有兴趣将其保存在文件中,请在终端中键入以下命令

 ping google.in | xargs -n1 -i bash -c 'echo `date +"%Y-%m-%d %H:%M:%S"`" {}"' >> "/home/name_of_your_computer/Desktop/Ping_Test.txt" 

您不需要创建任何文本文件,它会自动执行

Ping_Test.txt

 2018-04-19 15:35:53 PING google.in (216.58.203.164) 56(84) bytes of data. 2018-04-19 15:35:53 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=1 ttl=57 time=23.0 ms 2018-04-19 15:35:53 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=2 ttl=57 time=38.8 ms 2018-04-19 15:35:54 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=3 ttl=57 time=32.6 ms 2018-04-19 15:35:55 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=4 ttl=57 time=22.2 ms 2018-04-19 15:35:56 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=5 ttl=57 time=22.1 ms 2018-04-19 15:35:59 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=7 ttl=57 time=23.6 ms 2018-04-19 15:36:00 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=8 ttl=57 time=22.6 ms 2018-04-19 15:36:01 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=9 ttl=57 time=22.3 ms 2018-04-19 15:36:02 64 bytes from bom07s11-in-f4.1e100.net (216.58.203.164): icmp_seq=10 ttl=57 time=26.3 ms 

(感谢Achu和Eliah Kagan提出的想法),有一种方法可以实现

  • ping输出添加时间
  • 保持ping脚注
  • 并使用ctrl+c终止整个构造

要做到这一点,应该指示命令的正确部分(在管道之后)使用trap "" INT忽略SIGINT

 $ ping www.google.fr | bash -c 'trap "" INT; awk "{print strftime(\"%c - \") \$0}"' lun 26 Mar 2018 22:05:08 +0300 - PING www.google.fr (173.194.73.94) 56(84) bytes of data. lun 26 Mar 2018 22:05:08 +0300 - 64 bytes from lq-in-f94.1e100.net (173.194.73.94): icmp_seq=1 ttl=47 time=19.6 ms lun 26 Mar 2018 22:05:09 +0300 - 64 bytes from lq-in-f94.1e100.net (173.194.73.94): icmp_seq=2 ttl=47 time=20.1 ms ^Clun 26 Mar 2018 22:05:09 +0300 - lun 26 Mar 2018 22:05:09 +0300 - --- www.google.fr ping statistics --- lun 26 Mar 2018 22:05:09 +0300 - 2 packets transmitted, 2 received, 0% packet loss, time 1000ms lun 26 Mar 2018 22:05:09 +0300 - rtt min/avg/max/mdev = 19.619/19.866/20.114/0.284 ms