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

 
由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,
由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的几个shell命令组合
就可以实现:长时间ping一个地址,记录每次ping的时间戳,并输出到文本保存,另外我们
还可以将这个动作放到后台去执行,以免登陆注销之后被中断。
 
首先是长时间ping,这个非常简单,使用参数-c即可:
 
[root@test ~]# 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 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.341 ms
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.291 ms
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.259 ms
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.338 ms
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.339 ms
64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.243 ms
64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.234 ms
64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.333 ms
64 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 9002ms
rtt min/avg/max/mdev = 0.234/0.330/0.638/0.109 ms
上面我们ping了10次,每次的时间1秒,因此比如你要ping连天那么就是60*60*24*2=172800。
接下来是加时间戳:
 
root@test ~]# 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:21
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.436 ms    10:30:21
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.343 ms    10:30:22
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.368 ms    10:30:23
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.280 ms    10:30:24
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.308 ms    10:30:25
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.360 ms    10:30:26
64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.319 ms    10:30:27
64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.274 ms    10:30:28
64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.360 ms    10:30:29
64 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:30
10 packets transmitted, 10 received, 0% packet loss, time 9000ms    10:30:30
rtt min/avg/max/mdev = 0.265/0.331/0.436/0.052 ms    10:30:30
 
然后我们把信息输出到文本:
[root@test ~]# ping 192.168.2.1 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '>ping.log 
[root@test ~]# cat ping.log
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.    10:37:23
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.398 ms    10:37:23
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.288 ms    10:37:24
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.465 ms    10:37:25
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.310 ms    10:37:26
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.275 ms    10:37:27
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.247 ms    10:37:28
64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.339 ms    10:37:29
64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.270 ms    10:37:30
64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.297 ms    10:37:31
64 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:32
10 packets transmitted, 10 received, 0% packet loss, time 9000ms    10:37:32
rtt min/avg/max/mdev = 0.247/0.317/0.465/0.067 ms    10:37:32

最新文章

  1. C语言之链表list
  2. jmeter agent配置
  3. Eclipse 启动出现错误 no java virtual machine was found
  4. Oracle中三种循环(For、While、Loop)
  5. Code::Blocks配置GTK+2和GTK+3
  6. js时间格式化(yy年MM月dd日 hh:mm)
  7. PHP json_decode 函数解析 json 结果为 NULL 的解决方法
  8. MyEclipse 10优化技巧
  9. HDU 2045 不容易系列之(3)—— LELE的RPG难题(递推)
  10. Grid (read-only) objects and methods (client-side reference)获取子表单对象的一些方法 Crm 2016
  11. iview 将table的selection多选变单选方法
  12. hdu5965扫雷 枚举+递推
  13. UOJ#416. 【APIO2018】铁人两项
  14. HTML第四耍 超链接标签
  15. 002.Ceph安装部署
  16. Java Web 项目目录结构
  17. sql中优化查询
  18. 美国谍梦第一季/全集The Americans迅雷下载
  19. 完美解决office2013 错误1402
  20. SAX,功能强大的 API

热门文章

  1. CGI, FCGI, SCGI, WSGI 释异
  2. FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL
  3. Django怎么获取get请求里面的参数
  4. Strategy(策略)
  5. Flyweight(享元)
  6. QWebView_QWebEngineView
  7. 同样是搞Java,年薪15W和50W到底差在哪里?
  8. python学习笔记(断言assert)
  9. 定义集合属性(WPF)
  10. js中字符串处理成数字的方法