Linux下长时间ping网络加时间戳并记录到文本

由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的几个shell命令组合就可以实现:长时间ping一个地址,记录每次ping的时间戳,并输出到文本保存,另外我们还可以将这个动作放到后台去执行,以免登陆注销之后被中断。

首先是长时间ping,,这个非常简单,使用参数-c即可:

[root@ ~]# ping 192.168.2.1 -c 10 PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.638 ms64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.341 ms64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.291 ms64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.259 ms64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.338 ms64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.339 ms64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.243 ms64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.234 ms64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.333 ms64 bytes from 192.168.2.1: icmp_seq=10 ttl=64 time=0.284 ms

— 192.168.2.1 ping statistics —10 packets transmitted, 10 received, 0% packet loss, time 9002msrtt min/avg/max/mdev = 0.234/0.330/0.638/0.109 ms上面我们ping了10次,每次的时间1秒,因此比如你要ping连天那么就是60*60*24*2=172800。接下来是加时间戳:root@ ~]# ping 192.168.2.1 -c 10 | awk ‘{ print $0″\t” strftime(“%H:%M:%S”,systime()) } ‘PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data. 10:30:2164 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.436 ms 10:30:2164 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.343 ms 10:30:2264 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.368 ms 10:30:2364 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.280 ms 10:30:2464 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.308 ms 10:30:2564 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.360 ms 10:30:2664 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.319 ms 10:30:2764 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.274 ms 10:30:2864 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.360 ms 10:30:2964 bytes from 192.168.2.1: icmp_seq=10 ttl=64 time=0.265 ms 10:30:30 10:30:30— 192.168.2.1 ping statistics — 10:30:3010 packets transmitted, 10 received, 0% packet loss, time 9000ms 10:30:30rtt min/avg/max/mdev = 0.265/0.331/0.436/0.052 ms 10:30:30然后我们把信息输出到文本:[root@ ~]# ping 192.168.2.1 -c 10 | awk ‘{ print $0″\t” strftime(“%H:%M:%S”,systime()) } ‘>ping.log[root@ ~]# cat ping.logPING 192.168.2.1 (192.168.2.1) 56(84) bytes of data. 10:37:2364 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.398 ms 10:37:2364 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.288 ms 10:37:2464 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.465 ms 10:37:2564 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.310 ms 10:37:2664 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.275 ms 10:37:2764 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.247 ms 10:37:2864 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.339 ms 10:37:2964 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.270 ms 10:37:3064 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.297 ms 10:37:3164 bytes from 192.168.2.1: icmp_seq=10 ttl=64 time=0.289 ms 10:37:32 10:37:32— 192.168.2.1 ping statistics — 10:37:3210 packets transmitted, 10 received, 0% packet loss, time 9000ms 10:37:32rtt min/avg/max/mdev = 0.247/0.317/0.465/0.067 ms 10:37:32最后,我们需要把任务放到后台去:[root@ ~]# nohup ping 192.168.2.1 -c 10 | awk ‘{ print $0″\t” strftime(“%H:%M:%S”,systime()) } ‘>ping1.log &[1] 2616[root@ ~]# lsanaconda-ks.cfg check1.sh Desktop eygle.com httpd login pass.conf ping1.log ping.log test1.sh test1.sh1[root@ ~]# cat ping1.log PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data. 10:40:2264 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.373 ms 10:40:2264 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.343 ms 10:40:2364 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.335 ms 10:40:2464 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.299 ms 10:40:2564 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.372 ms 10:40:2664 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.236 ms 10:40:2764 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.394 ms 10:40:2864 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.317 ms 10:40:2964 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.490 ms 10:40:3064 bytes from 192.168.2.1: icmp_seq=10 ttl=64 time=1.65 ms 10:40:31 10:40:31— 192.168.2.1 ping statistics — 10:40:3110 packets transmitted, 10 received, 0% packet loss, time 9001ms 10:40:31rtt min/avg/max/mdev = 0.236/0.480/1.650/0.395 ms 10:40:31

-The End-

人生的路无需苛求。只要你迈步,路就在你的脚下延伸。

Linux下长时间ping网络加时间戳并记录到文本

相关文章:

你感兴趣的文章:

标签云: