免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 23467 | 回复: 7
打印 上一主题 下一主题

[网络管理] ping命令如何加时间戳! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-16 15:39 |只看该作者 |倒序浏览
要求~~我要做个网络测试..
将一直ping着网关...并将结果写到一个文件里....

但没有时间戳~!!我不知道什么时间发的ping包.

因为要连续PING好几天.........

请问怎么在ping中加入时间戳.

论坛徽章:
34
亥猪
日期:2015-03-20 13:55:11戌狗
日期:2015-03-20 13:57:01酉鸡
日期:2015-03-20 14:03:56未羊
日期:2015-03-20 14:18:30子鼠
日期:2015-03-20 14:20:14丑牛
日期:2015-03-20 14:20:31辰龙
日期:2015-03-20 14:35:34巳蛇
日期:2015-03-20 14:35:56操作系统版块每日发帖之星
日期:2015-11-06 06:20:00操作系统版块每日发帖之星
日期:2015-11-08 06:20:00操作系统版块每日发帖之星
日期:2015-11-19 06:20:00黄金圣斗士
日期:2015-11-24 10:43:13
2 [报告]
发表于 2009-03-16 16:39 |只看该作者
date;ping >>
?

论坛徽章:
0
3 [报告]
发表于 2009-03-16 17:09 |只看该作者
while true
do
date >>/tmp/ping.log
ping -c 10000 192.168.1.1 >>/tmp/ping.log
done

每ping10000个包就重新加个时间戳,参考一下吧

论坛徽章:
0
4 [报告]
发表于 2009-03-16 23:17 |只看该作者

回复 #3 copywu 的帖子

简单的组合就可以实现

论坛徽章:
0
5 [报告]
发表于 2011-11-13 13:24 |只看该作者
要求~~我要做个网络测试..
将一直ping着网关...并将结果写到一个文件里....

但没有时间戳~!!我不知道什 ...
itasen 发表于 2009/03/16 15:39



    我来一个简单的:
while true; do echo `date` $(ping -c 1 g.cn |grep "^64 bytes from"); sleep 1; done
如果想后台写到文件中:(或者你会需要记下其PID)
while true; do echo `date` $(ping -c 1 g.cn |grep "^64 bytes from") >>/tmp/ping.log; sleep 1; done &

论坛徽章:
0
6 [报告]
发表于 2011-11-30 15:55 |只看该作者
刚写的代码:

ping 192.168.5.160 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '

调用awk中的时间处理函数strftime给每个测量值加上时间戳。
结果如下:

PING 192.168.5.160 (192.168.5.160) 56(84) bytes of data.        15:51:38
64 bytes from 192.168.5.160: icmp_seq=1 ttl=64 time=0.245 ms        15:51:38
64 bytes from 192.168.5.160: icmp_seq=2 ttl=64 time=0.192 ms        15:51:39
64 bytes from 192.168.5.160: icmp_seq=3 ttl=64 time=0.190 ms        15:51:40
64 bytes from 192.168.5.160: icmp_seq=4 ttl=64 time=0.215 ms        15:51:41
64 bytes from 192.168.5.160: icmp_seq=5 ttl=64 time=0.214 ms        15:51:42
64 bytes from 192.168.5.160: icmp_seq=6 ttl=64 time=0.213 ms        15:51:43
64 bytes from 192.168.5.160: icmp_seq=7 ttl=64 time=0.209 ms        15:51:44
64 bytes from 192.168.5.160: icmp_seq=8 ttl=64 time=0.209 ms        15:51:45
64 bytes from 192.168.5.160: icmp_seq=9 ttl=64 time=0.206 ms        15:51:46
64 bytes from 192.168.5.160: icmp_seq=10 ttl=64 time=0.204 ms        15:51:47
        15:51:47
--- 192.168.5.160 ping statistics ---        15:51:47
10 packets transmitted, 10 received, 0% packet loss, time 8997ms        15:51:47
rtt min/avg/max/mdev = 0.190/0.209/0.245/0.022 ms        15:51:47

不足之处是给每行都加了个时间戳。

论坛徽章:
0
7 [报告]
发表于 2011-12-01 16:52 |只看该作者
本帖最后由 gongguan 于 2011-12-01 16:55 编辑

如果长时间ping可以

[root@samba ~]# pwd
/root
[root@samba ~]# nohup ping 192.168.0.1 &
[1] 27436

[root@samba ~]# crontab -e
*/2 * * * *     date  >>  /root/nohup.out

得出的结果如下:
64 bytes from 192.168.0.1: icmp_seq=122 ttl=128 time=0.546 ms
64 bytes from 192.168.0.1: icmp_seq=123 ttl=128 time=0.728 ms
64 bytes from 192.168.0.1: icmp_seq=124 ttl=128 time=0.661 ms
64 bytes from 192.168.0.1: icmp_seq=125 ttl=128 time=0.593 ms
64 bytes from 192.168.0.1: icmp_seq=126 ttl=128 time=0.524 ms
64 bytes from 192.168.0.1: icmp_seq=127 ttl=128 time=0.708 ms
64 bytes from 192.168.0.1: icmp_seq=128 ttl=128 time=0.641 ms
64 bytes from 192.168.0.1: icmp_seq=129 ttl=128 time=0.572 ms
64 bytes from 192.168.0.1: icmp_seq=130 ttl=128 time=0.505 ms
64 bytes from 192.168.0.1: icmp_seq=131 ttl=128 time=0.689 ms
64 bytes from 192.168.0.1: icmp_seq=132 ttl=128 time=0.622 ms
Thu Dec  1 16:50:01 CST 2011
64 bytes from 192.168.0.1: icmp_seq=133 ttl=128 time=0.550 ms
64 bytes from 192.168.0.1: icmp_seq=134 ttl=128 time=0.485 ms
64 bytes from 192.168.0.1: icmp_seq=135 ttl=128 time=0.670 ms
64 bytes from 192.168.0.1: icmp_seq=136 ttl=128 time=0.601 ms
64 bytes from 192.168.0.1: icmp_seq=137 ttl=128 time=0.535 ms
64 bytes from 192.168.0.1: icmp_seq=138 ttl=128 time=0.718 ms
64 bytes from 192.168.0.1: icmp_seq=139 ttl=128 time=0.651 ms
64 bytes from 192.168.0.1: icmp_seq=140 ttl=128 time=0.583 ms
64 bytes from 192.168.0.1: icmp_seq=141 ttl=128 time=0.516 ms
64 bytes from 192.168.0.1: icmp_seq=142 ttl=128 time=0.698 ms
64 bytes from 192.168.0.1: icmp_seq=143 ttl=128 time=0.632 ms
64 bytes from 192.168.0.1: icmp_seq=144 ttl=128 time=0.564 ms
64 bytes from 192.168.0.1: icmp_seq=145 ttl=128 time=0.747 ms

比较适合长时间监测。  多长时间插入时间戳  自己定义。

论坛徽章:
0
8 [报告]
发表于 2012-10-29 16:54 |只看该作者
Set shell = WScript.CreateObject("WScript.Shell")
Set re=New RegExp
re.Pattern = "^Reply|^Request"
‘将下面的IP改成你想要ping的IP.
Set myping=shell.Exec("ping 192.168.0.1")
while Not myping.StdOut.AtEndOfStream
        strLine = myping.StdOut.ReadLine()
        r=re.Test(strLine)
        If r Then
                WScript.Echo date & " "& time & chr(9) & strLine
        End if
Wend
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP