[shell] Obtain ping delay data and analyze network conditions and learn common commands

Obtain ping delay data and analyze network conditions

The network situation often gives us a headache. It is too troublesome to manually ping the terminal every time. It is better to write a script to ping and save the data with a time stamp in a file, and then analyze which time period the network is relatively poor.

Create a demo.shfile:

#!/bin/bash
# 清理日志
net_path="./network/"

# 目录存在,删除修改时间为1天前的文件
if  [ -d "${net_path}" ]; then 
echo start delete log 1 days ago...
    find "${net_path}"/* -name '*.txt' -mtime +1 -exec rm -rf {
    
    } \;
echo end delete log ...	
fi

output_file="./network/output.txt"
# 设置目标主机
target_host="baidu.com"

# 定义函数获取当前时间戳
get_timestamp() {
    
    
    date +"%Y-%m-%d %H:%M:%S"
}

# 循环执行ping命令
while true; do
    # 获取当前时间戳
    timestamp=$(get_timestamp)
    
    # 执行ping命令并抓取延迟时间
    ping_result=$(ping -c 1 $target_host | grep time= | awk -F 'time=' '{print $2}' | cut -d ' ' -f 1)
    
    # 输出带有时间戳的ping结果
    echo "$timestamp - Ping: $ping_result ms"
    echo "$timestamp - Ping: $ping_result ms" >> "$output_file"
    
    # 等待1秒后继续执行
    sleep 1
done

The time-delayed data will be saved output.txt, and then the files from the previous day will be automatically deleted.

Finally, you can import the txt into excel to generate a chart to see when the network is poor:

insert image description here

Analysis of |, ||, &, &&

The vertical bar '|' means the pipe character in Linux, and the output of the command before '|' is used as the input after '|';

Double vertical lines '||', multiple commands separated by double vertical lines '||', follow the following rules when executing: if the previous command is true, the following command will not be executed, if the previous command is false , then continue to execute the following commands;

& Execute multiple commands at the same time, regardless of whether the command is executed successfully or not;

&& can execute multiple commands at the same time. When an incorrect command is encountered, the subsequent commands will not be executed. If there is no error all the time, the execution is complete.

When using it, remember that '|' is a pipe character, & is parallel execution, and '||' and && are their advanced versions respectively.

tee

tee is a file management command. The tee command is used to read standard input data and output its content into a file. like:

tee 1.txt
ls -l | tee 2.txt

Can be used to print terminal output and logs etc.

tail

The tail command can be used to view the contents of the file. There is a common parameter -f, which is often used to view the log file that is being changed. like:

tail 1.txt
tail -f 1.txt

Can be used to view log file changes.

killall

The kill command kills the specified process PID and needs to be used with ps, while killall directly operates on the name of the process, which is more convenient. kill is often followed by the PID code, and killall is often followed by the process name. like:

kill -9 8178
killall -9 bash
killall -9 roscore
killall -9 rosmaster

Can be used to exit a process.

that's all.

Guess you like

Origin blog.csdn.net/qq_40344790/article/details/132027316