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 www.2cto.com
[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
最后,我们需要把任务放到后台去:
[root@test ~]# nohup ping 192.168.2.1 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '>ping1.log &
[1] 2616
[root@test ~]# ls
anaconda-ks.cfg check1.sh Desktop eygle.com httpd login pass.conf ping1.log ping.log test1.sh test1.sh1 www.2cto.com
[root@test ~]# cat ping1.log
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data. 10:40:22
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.373 ms 10:40:22
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.343 ms 10:40:23
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.335 ms 10:40:24
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.299 ms 10:40:25
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.372 ms 10:40:26
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.236 ms 10:40:27
64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.394 ms 10:40:28
64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.317 ms 10:40:29
64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.490 ms 10:40:30
64 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:31
10 packets transmitted, 10 received, 0% packet loss, time 9001ms 10:40:31
rtt min/avg/max/mdev = 0.236/0.480/1.650/0.395 ms 10:40:31
-The End-
- Linux下长时间ping网络加时间戳并记录到文本(转)
[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 f ...
- CentOS下长时间ping网络加时间戳并记录到文本
Linux下长时间ping网络加时间戳并记录到文本 由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...
- linux下的shell运算(加、减、乘、除)
linux下的shell运算(加.减.乘.除) 标签: linuxshell运算加减乘除 2014-03-12 16:25 15127人阅读 评论(0) 收藏 举报 分类: linux(17) ((i ...
- Linux下c函数dlopen实现加载动态库so文件代码举例
dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...
- 在Linux下用netstat查看网络状态、端口状态
在Linux下用netstat查看网络状态.端口状态 在linux一般使用netstat 来查看系统端口使用情况步. netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实 ...
- linux下C语言socket网络编程简例
原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...
- Linux下精确控制时间的函数
Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...
- [转载]linux下网卡漂移导致网络不可用
转自:https://blog.csdn.net/hyatsz/article/details/47690993 linux下网卡漂移导致网络不可用 2015年08月16日 00:48:50 hyat ...
- Linux下设置时间
Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...
随机推荐
- oracle树形查询 start with connect by
一.简介 在oracle中start with connect by (prior) 用来对树形结构的数据进行查询.其中start with conditon 给出的是数据搜索范围, connect ...
- 【转】深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
[原文]:http://www.jb51.net/article/39432.htm 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. ...
- sqlserver如何创建镜像图文教程(转)
由于工作中需要做SQL的镜像异地备份,以前都没有研究过,百度了一个文章记录下,方便以后查询 转载地址:http://jingyan.baidu.com/article/d5c4b52b20843fda ...
- Java IO工作机制分析
Java的IO类都在java.io包下,这些类大致可分为以下4种: 基于字节操作的 I/O 接口:InputStream 和 OutputStream 基于字符操作的 I/O 接口:Writer 和 ...
- python 聊天室
server端程序 # -*- coding: utf-8 -*- #!/usr/bin/python """ """ import soc ...
- 【2016-11-6】【坚持学习】【Day21】【子窗口关闭时,同步关闭它的主窗口(方法二)】
根据上文,在子窗口设置一个委托.然后在子窗口关闭事件,执行委托实例,然后在主窗口增加监听委托的方法.... 想想,本事关闭事件就是一个特殊的委托.那么干嘛还要特意去声明一个新的呢?多此一举. 于是有下 ...
- python3 下的文件输入输出特性以及如何覆盖文件内容和接下去输入
今天碰到了一个非常有意思的python特性.本来我是想打开一个文件,在文件的末尾接下去输入一些内容的,代码如下: f = open('test.txt', 'r+') f.write(content) ...
- HDU3038 How Many Answers Are Wrong[带权并查集]
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- CH Round #52 还教室[线段树 方差]
还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...
- COGS247. 售票系统[线段树 RMQ]
247. 售票系统 ★★☆ 输入文件:railway.in 输出文件:railway.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 某次列车途经C个城市,城市 ...