ping add time output

Introduction to ping

Ping is a computer network tool used to test whether a data packet can reach a specific host through the IP protocol. The working principle of ping is to send an ICMP request echo data packet to the target host, and wait for the echo response data packet to be received. The program estimates the packet loss rate (packet loss rate) and packet round-trip time (network delay, Round-trip delay time) based on time and the number of successful responses.

Just ping ipdo it.

If pingecho time is displayed, this command also provides parameter -Dto echo timestamp.

rues@rues-virtual-machine:~$ ping www.baidu.com -D
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
[1673402731.237683] 64 bytes from 14.215.177.39: icmp_seq=1 ttl=128 time=34.0 ms
[1673402732.202997] 64 bytes from 14.215.177.39: icmp_seq=2 ttl=128 time=33.5 ms
[1673402733.204868] 64 bytes from 14.215.177.39: icmp_seq=3 ttl=128 time=33.0 ms
[1673402734.207801] 64 bytes from 14.215.177.39: icmp_seq=4 ttl=128 time=34.5 ms
[1673402735.208844] 64 bytes from 14.215.177.39: icmp_seq=5 ttl=128 time=33.3 ms
[1673402736.211071] 64 bytes from 14.215.177.39: icmp_seq=6 ttl=128 time=33.9 ms

However, the readability of the timestamp is poor. Although some online tools ( unitxtime ) can be used to convert it, it is cumbersome. The best way is to echo it in a more readable time format.

Use of the ping command

Common parameters

-i: The interval between each ping operation, the default is 1s;

-c: The number of times to perform ping operations, the default is always executed, unless interrupted;

-s: Specify the size of the packet sent when performing the ping operation, the default is 56B, after adding the header, the final sent is 64B.

# 在终端 ping 某个地址, 执行10次
ping baidu.com -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=40 time=83.3 ms       10:41:23
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.4 ms       10:41:24
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.4 ms       10:41:25

# 日期在后面
ping baidu.com | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}'
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=40 time=83.2 ms       2021-06-09 10:42:45
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.3 ms       2021-06-09 10:42:46
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.3 ms       2021-06-09 10:42:47

# 日前在前面
ping baidu.com | awk '{ print strftime("%Y.%m.%d %H:%M:%S",systime())"\t" $0; fflush() }'
2021.06.09 10:43:28     64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=46 time=162 ms
2021.06.09 10:43:29     64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=46 time=177 ms
2021.06.09 10:43:30     64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=46 time=174 ms

ping redirects the output to the specified file

use fflush

Note: Use fflush(), otherwise the file will not have information, because awk also has a cache.

In order to prevent the script from being interrupted, the script can be executed in the background through nohup:

# 下面未加fflush(),执行命令生成文件会等一会才会有信息打印到文件里
nohup ping baidu.com | awk '{ print strftime("%Y-%m-%d %H:%M:%S",systime())"\t" $0; fflush() }' >> long_ping.txt &
$ tail -f long_ping.txt 
2021-06-09 10:45:54     64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.3 ms
2021-06-09 10:45:55     64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.3 ms
2021-06-09 10:45:56     64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=40 time=83.3 ms

# 要结束后台进程, 可通过下述方式查找并kill
$ ps -ef |grep ping
user00    5778 30382  0 10:45 pts/2    00:00:00 ping baidu.com
user00    7133 30382  0 10:48 pts/2    00:00:00 grep --color=auto ping
$ kill -9 5778
[1]+  Done                    nohup ping baidu.com | awk '{ print strftime("%Y.%m.%d %H:%M:%S",systime())"\t" $0; fflush() }' >> long_ping.txt

use pong

  1. What is pingpong?

Pingpong is a means of data caching, and the efficiency of data transmission can be improved through pingpong operation.

  1. When do you need pingpong?

When exchanging data between two modules, the results of the upper level processing cannot be completed by the lower level immediately, so the upper level must wait for the completion of the lower level processing before sending new data, which will have a great impact on performance Big loss.

After the introduction of pingpong, we can save the result in the cache of the pong road instead of waiting for the end of the next level of processing. When the data of the pong road is ready, the data of the ping road is also processed (the next level), and then there is no need to Wait for the pong road data to be processed directly, and the upper level does not need to wait, and instead stores the result in the ping road. This improves processing efficiency.

nohup ping baidu.com -i 1 | while read pong; do echo "$(date +"%Y-%m-%d %H:%M:%S") | $pong"; done | tee -a ping-baidu.com.log &

date timestamp

Data stamp conversion

date can convert timestamp to localtime.

# date -d @1623205723.324359
Wed Jun  9 10:28:43 CST 2021
# date [email protected]
Wed Jun  9 10:28:43 CST 2021
利用awk进行转化,比较麻烦。

awk splicing

# 格式可以自定义调整
ping baidu.com | awk '{"date" | getline date; print date,$0}'
Wed Jun  9 10:33:01 CST 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.3 ms
Wed Jun  9 10:33:01 CST 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.5 ms
Wed Jun  9 10:33:01 CST 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=40 time=83.3 ms

# 时间格式可根据date自定义
ping baidu.com | awk -v date="$(date +"%Y-%m-%d %r")" '{print date, $0}'
2021-06-09 10:33:38 AM 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=40 time=83.3 ms
2021-06-09 10:33:38 AM 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.5 ms
2021-06-09 10:33:38 AM 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.6 ms

perl

If awk does not have strftime()

Notice: An error "Can't locate Time/Piece.pm in @INC" is reported, and the command needs to be executed yum -y install perl-Time-Pieceto install the necessary packages.

# 要将其重定向到文件,请使用标准shell重定向并关闭输出缓冲:
ping baidu.com | perl -nle 'print scalar(localtime), " ", $_'
Wed Jun  9 10:36:14 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.4 ms
Wed Jun  9 10:36:15 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.5 ms
Wed Jun  9 10:36:16 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=40 time=83.3 ms

# 如果显示ISO8601时间格式
ping baidu.com | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_'
Wed Jun  9 10:36:41 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=40 time=83.3 ms
Wed Jun  9 10:36:42 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.3 ms
Wed Jun  9 10:36:43 2021 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.5 ms

ping baidu.com | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_'
2021-06-09T10:37:08 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=40 time=83.4 ms
2021-06-09T10:37:09 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=40 time=83.4 ms
2021-06-09T10:37:10 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=40 time=83.2 ms

Replenish

strftime补充:
函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
%a 星期几的简写
%A 星期几的全称
%b 月份的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的前两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年份,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数

Guess you like

Origin blog.csdn.net/qq_38269618/article/details/128640473