Shell Long ping script monitoring network status

The linux environment uses ping command + timestamp to record in the file

In the production environment, whether the network is stable (network delay) is a very important index. In order to check the network delay conveniently, we can realize long-term network monitoring by ping command.


1, Use of ping command

1. Common parameters

-i: The interval between ping operations is 1s by default;

-c: The number of times the ping operation is executed. By default, it is always executed unless interrupted;

-s: Specifies the size of the packet sent during the ping operation. The default is 56B. After the header is added, the final packet is 64B


2. Use example

#ping an address at the terminal, execute 10 times
# ping 112.80.248.75 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '

#If the background operation is recorded in the file, but the terminal cannot be disconnected, the statement is as follows:
nohup ping 112.80.248.75 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } '>ping1.log &


2.1 the date and time are later:

# ping 112.80.248.75 | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}' >> long_ping.txt

PING 112.80.248.75 (112.80.248.75) 56(84) bytes of data.       2020-04-03 16:59:44
64 bytes from 112.80.248.75: icmp_seq=1 ttl=55 time=3.66 ms     2020-04-03 16:59:44
64 bytes from 112.80.248.75: icmp_seq=2 ttl=55 time=4.90 ms     2020-04-03 16:59:45
64 bytes from 112.80.248.75: icmp_seq=3 ttl=55 time=4.02 ms     2020-04-03 16:59:46


2.2. Date and time in front:

# ping 112.80.248.75 | awk '{ print strftime("%Y.%m.%d %H:%M:%S",systime())"\t" $0; fflush() }' >> long_ping.txt

2020.04.03 17:00:36     PING 112.80.248.75 (112.80.248.75) 56(84) bytes of data.
2020.04.03 17:00:36     64 bytes from 112.80.248.75: icmp_seq=1 ttl=55 time=3.22 ms
2020.04.03 17:00:37     64 bytes from 112.80.248.75: icmp_seq=2 ttl=55 time=3.72 ms
2020.04.03 17:00:38     64 bytes from 112.80.248.75: icmp_seq=3 ttl=55 time=4.91 ms
2020.04.03 17:00:39     64 bytes from 112.80.248.75: icmp_seq=4 ttl=55 time=3.95 ms

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

The following does not add fflush(), the file generated by executing the command will wait for a while before information is printed to the file

ping 112.80.248.75 | awk '{ print strftime("%Y.%m.%d %H:%M:%S",systime())"\t" $0 }'>> long_ping.txt &


2, Time stamp recorded by script

In order to facilitate later viewing and prevent the command from being interrupted when exiting the terminal, we can operate by running the command (script) in the background

The script is as follows:

# vi long_ping.sh 
#!/bin/bash
ping 112.80.248.75 | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}' >> long_ping.txt

Note: the awk command will output the result to the file only when the script is finished (or kill ed)

To prevent the script from being interrupted, the script can be executed in the background through nohup:

nohup sh long_ping.sh &

To end a background process, find and kill:

# ps -ef |grep long
root 17341 17236  0 16:46 pts/3 00:00:00 sh long_ping.sh
root 17351 17236  0 16:47 pts/3 00:00:00 grep --color=auto long


Tags: Linux network

Posted on Fri, 03 Apr 2020 10:19:19 -0700 by Coruba67