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


39

当我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

您将如何在命令行中执行此操作(如果可能)?


Answers:


77

采用:

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

您将得到如下结果:

在此处输入图片说明


1
哇,很棒。并有助于学习shell知识。谢谢!
奥利维尔·庞斯

正常运行时间以秒为单位ping 192.168.70.1 | while read pong; do echo "$(awk '{print $1}' /proc/uptime): $pong"; done
dps

16

使用该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

6

有一个名为的实用程序ts,它将读取stdin,添加时间戳并将其写入stdout:

me@my-laptop:~$ ping localhost | ts
Nov 08 09:15:41 PING localhost (127.0.0.1) 56(84) bytes of data.
Nov 08 09:15:41 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.060 ms
Nov 08 09:15:42 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.098 ms
Nov 08 09:15:43 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.082 ms
Nov 08 09:15:44 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.091 ms

可以使用安装在Ubuntu中。sudo apt install moreutils


1
+1就像一个旁注apt install moreutils
-ts

3

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

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

这与Achu的答案中的方法类似,但是其ping输出通过管道传递到gawk而不是调用的Shell循环date。与该方法一样,它在不使用的情况下也可以使用-c,但是如果您在n次 ping 之后没有通过使ping停止,并且使用+ 停止了循环,则不会打印通常的统计信息。-c nCtrlCping

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循环,都会发生这种情况。原因是管道的右侧的命令(而不是)在按+时会ping收到SIGINT,并且在终止之前不知道要打印统计信息。CtrlCping

如果您ping没有-c在管道的左侧运行(如上所示),并且希望以仍然打印统计信息的方式终止它,则可以在运行该终端的终端上按Ctrl+ 而不是从另一个终端,替换为命令的进程ID 。如果您仅运行的一个实例,则可以简单地使用。Ckill -INT PIDPIDpingpingkillall -INT ping

或者,您可以用以下ping命令替换管道左侧的命令:运行外壳程序,报告该外壳程序的进程ID,然后将该ping命令替换为该外壳程序(以使其具有相同的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,以替换7557为看到的实际进程ID,以使ping命令打印统计信息的方式终止该命令。

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

进一步阅读:


有没有一种方法可以先将ctrl + c发送到ping?
阿蒂

1
@atti是的,您可以使用或专门发送SIGINT(按Ctrl + C组合键)到ping进程。我已经详细说明了这个答案。killkillall
伊莱亚·卡根

很好使用awk。+1
ripat

0
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

-1

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

  • 增加ping输出时间
  • 保留脚注 ping
  • 并终止整个施工 ctrl+c

为此,应指示命令的正确部分(在管道之后)SIGINT使用忽略trap "" INT

$ 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.