平 是一个命令行网络管理实用程序,可用于几乎所有操作系统,包括嵌入式网络设备。 它主要用于测试网络上主机的连通性或可达性。 默认情况下,ping 命令不会在其输出中显示时间戳。 如果您出于任何原因想要使用 ping 命令输出打印时间戳,本教程将向您展示如何操作。
在 Linux 中使用 Ping 命令输出打印时间戳
显示时间戳 ping
命令的输出,只需传递 -D
像下面这样的标志。
$ ping -c 3 -D ostechnix.com
样本输出:
PING ostechnix.com(2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886)) 56 data bytes [1611825018.797092] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=1 ttl=57 time=94.8 ms [1611825019.616530] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=2 ttl=57 time=114 ms [1611825020.615700] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=3 ttl=57 time=112 ms [1611825021.596463] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=4 ttl=57 time=91.5 ms [1611825022.619467] 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=5 ttl=57 time=113 ms --- ostechnix.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4006ms rtt min/avg/max/mdev = 91.543/105.095/114.408/9.815 ms
正如您在上面的输出中看到的, ping
命令打印时间戳(unix时间+微秒,如 gettimeofday
) 在每一行之前。 就我而言, [1611825018.797092]
, [1611825019.616530]
…. [1611825022.619467]
是时间戳。
即使 Ping 命令具有启用时间戳的选项,输出也不是那么用户友好。 如果要以人类可读的格式显示时间戳,请使用 ts
命令如下节所示。
使用 ts 命令在 Ping 命令输出中启用时间戳
这 ts
命令是一部分 更多实用程序 包裹。 它将时间戳打印到任何 Linux 命令输出中每一行的开头。
使用 ts
命令,您需要安装 moreutlis 包,如以下链接所示:
- Moreutils – 更多有用的 Unix 实用程序的集合
现在,您可以在输出的每一行中显示时间戳 ping
命令如下:
$ ping -c 5 ostechnix.com | ts
样本输出:
Jan 28 16:38:40 PING ostechnix.com(2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886)) 56 data bytes Jan 28 16:38:40 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=1 ttl=57 time=76.7 ms Jan 28 16:38:41 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=2 ttl=57 time=93.2 ms Jan 28 16:38:42 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=3 ttl=57 time=84.8 ms Jan 28 16:38:43 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=4 ttl=57 time=84.3 ms Jan 28 16:38:44 64 bytes from 2606:4700:20::ac43:4886 (2606:4700:20::ac43:4886): icmp_seq=5 ttl=57 time=108 ms Jan 28 16:38:44 Jan 28 16:38:44 --- ostechnix.com ping statistics --- Jan 28 16:38:44 5 packets transmitted, 5 received, 0% packet loss, time 4006ms Jan 28 16:38:44 rtt min/avg/max/mdev = 76.718/89.390/107.965/10.646 ms
你注意到每行开头的时间戳了吗? 是的,它们是时间戳,现在很容易理解。 不仅对于 ping 命令, ts
command 可以显示任何命令输出的时间戳。
希望这可以帮助。
相关阅读:
- 使用 Gping 以图形格式显示 Ping 命令输出
- Prettyping – 使 Ping 命令的输出更漂亮,更易于阅读
- 如何在 Linux 中一次 Ping 多台主机
- Ping 多个服务器并在类似顶部的文本 UI 中显示输出