

为ping命令增加时间信息的正确方法
source link: https://www.lujun9972.win/blog/2021/04/26/%E4%B8%BAping%E5%91%BD%E4%BB%A4%E5%A2%9E%E5%8A%A0%E6%97%B6%E9%97%B4%E4%BF%A1%E6%81%AF%E7%9A%84%E6%AD%A3%E7%A1%AE%E6%96%B9%E6%B3%95/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

为ping命令增加时间信息的正确方法
不知道为什么,在网上搜索为 ping 命令增加时间信息方法时,给出来得解决方案都是将 ping 命令的结果传递给一个循环,然后在循环内生成时间。
这个循环可能是一个流式处理命令,比如 awk:
ping www.baidu.com -c 5 | awk '{ print strftime("%Y-%m-%d %H:%M:%S",systime())"\t"$0 }'
2021-04-26 11:26:24 PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data. 2021-04-26 11:26:24 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=52 time=12.1 ms 2021-04-26 11:26:24 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=52 time=9.51 ms 2021-04-26 11:26:25 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=52 time=18.4 ms 2021-04-26 11:26:26 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=52 time=15.4 ms 2021-04-26 11:26:27 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=52 time=9.81 ms 2021-04-26 11:26:27 2021-04-26 11:26:27 --- www.a.shifen.com ping statistics --- 2021-04-26 11:26:27 5 packets transmitted, 5 received, 0% packet loss, time 10077ms 2021-04-26 11:26:27 rtt min/avg/max/mdev = 9.519/13.089/18.481/3.432 ms
也可能是一个 while 循环语句:
ping www.baidu.com -c 5 |while read result do echo "$(date) ${result}" done
2021年 04月 26日 星期一 11:25:09 CST PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data. 2021年 04月 26日 星期一 11:25:09 CST 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=52 time=8.73 ms 2021年 04月 26日 星期一 11:25:09 CST 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=52 time=16.4 ms 2021年 04月 26日 星期一 11:25:10 CST 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=52 time=9.98 ms 2021年 04月 26日 星期一 11:25:11 CST 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=52 time=10.8 ms 2021年 04月 26日 星期一 11:25:12 CST 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=52 time=17.3 ms 2021年 04月 26日 星期一 11:25:12 CST 2021年 04月 26日 星期一 11:25:12 CST --- www.a.shifen.com ping statistics --- 2021年 04月 26日 星期一 11:25:12 CST 5 packets transmitted, 5 received, 0% packet loss, time 7043ms 2021年 04月 26日 星期一 11:25:12 CST rtt min/avg/max/mdev = 8.731/12.684/17.392/3.548 ms
但是这有个问题,那就是时间戳不是由 ping
命令生成的,而是在循环体内生成的。这就导致若我们在时间戳生成之前用 sed
和 awk
之类的命令对 ping
结果加工后,由于它们的缓存机制会使得输出到循环的时间比实际 ping 命令产生结果的时间产生较大差别。
例如下面命令的输出中,生成的时间是同一秒,这明显是不对的。
ping www.baidu.com -c 5|awk '1' | awk '{ print strftime("%Y-%m-%d %H:%M:%S",systime())"\t"$0 }'
2021-04-26 11:27:01 PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data. 2021-04-26 11:27:01 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=52 time=13.2 ms 2021-04-26 11:27:01 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=52 time=8.32 ms 2021-04-26 11:27:01 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=52 time=18.1 ms 2021-04-26 11:27:01 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=52 time=8.61 ms 2021-04-26 11:27:01 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=52 time=9.09 ms 2021-04-26 11:27:01 2021-04-26 11:27:01 --- www.a.shifen.com ping statistics --- 2021-04-26 11:27:01 5 packets transmitted, 5 received, 0% packet loss, time 4052ms 2021-04-26 11:27:01 rtt min/avg/max/mdev = 8.322/11.492/18.191/3.794 ms
事实上,通过查看 ping
命令的 manual, 我们可以发现 ping 命令的 -D
选项本身就会为每一行输出生成时间戳:
-D Print timestamp (unix time + microseconds as in gettimeofday) before each line.
ping -c 5 www.baidu.com -D
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data. [1619407749.478198] 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=52 time=8.86 ms [1619407749.492436] 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=52 time=13.8 ms [1619407750.493666] 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=52 time=13.4 ms [1619407751.492797] 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=52 time=10.5 ms [1619407752.501662] 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=52 time=17.1 ms --- www.a.shifen.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4042ms rtt min/avg/max/mdev = 8.861/12.776/17.120/2.861 ms
唯一的问题就是这个时间戳采取的是是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不方便理解,但是没关系,我们可以用 date
进行一下转换:
ping -c 5 www.baidu.com -D |awk '1' |while read result do if [[ "${result}" =~ "[" ]] # 以 [ 开头的行带时间戳 then read timestamp rest < <(echo ${result}|tr -d '[]') echo $(date -d @${timestamp}) "${rest}" else echo "${result}" fi done
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data. 2021年 04月 26日 星期一 11:41:25 CST 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=52 time=8.97 ms 2021年 04月 26日 星期一 11:41:25 CST 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=52 time=7.89 ms 2021年 04月 26日 星期一 11:41:26 CST 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=52 time=27.3 ms 2021年 04月 26日 星期一 11:41:27 CST 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=52 time=20.6 ms 2021年 04月 26日 星期一 11:41:28 CST 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=52 time=22.2 ms --- www.a.shifen.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4069ms rtt min/avg/max/mdev = 7.897/17.425/27.343/7.671 ms
Recommend
-
69
-a 解析主机ip地址,同时也会显示对方的NETBIOS名(在ip地址的前面)-n 发送count指定的ECHO数据包数,通过这个命令可以自己定义发送的个数,对衡量网络速度很有帮助。能够测试发送数据包的返回平均时间,及时间的快慢程度。默认值为 4。-l &a...
-
13
为 ping 命令添加可视化图形输出-gping – 开源派 gping是一款基于Rust编写的ping开源命令行工具。遵守MIT开源协议。gping可以给予 ping 命令添加可视化图形输出。
-
8
Linux之ping命令 原创 入门小站 2022-03-03 11:47:35 ©著作权...
-
6
使用 golang 实现 ping 命令0 条评论ping 是一个经常被用来检查主机间连通性的工具, 它基于 ICMP 协议实现, 基本原理很简单: 本机给远程机器发送...
-
5
ping命令使用图文教程更新日期: 2022-05-20阅读量: 23标签: 命令分...
-
2
迷途小书童的Note ping 命令主要用来测试主机之间网络的连通性。ping 使用的是
-
6
QT实现ping命令 精选 原创 五个板栗 2022-10-21 16:26:52...
-
11
nmap Linux nmap命令 ping扫描 当您要快速确定哪些指定主机已启动并正在运行时,ping扫描会非常有用 ...
-
1
Python 实现Ping命令状态检测 精选 原创 ping 是一种因特网包探索器,用于测试网络连接量的程序,Ping是工作在TCP/IP网络体系结构...
-
15
【笔记】Ubuntu安装Ping命令 | 绯鞠的博客 【笔记】Ubuntu安装Ping命令 2023-04-30 Ubuntu安装Ping命令学习笔记
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK