Linux 脚本记录 ping 的时间
2025-03-10 10:02:19    14    0    0
admin

将以下脚本内容保存为文件 ping_log.sh

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "Usage: $0 <IP_ADDRESS>"
    exit 1
fi

LOG_FOLDER=/var/log/ping

ip_address=$1

current_date=$(date +%Y%m%d)

log_file="${LOG_FOLDER}/${current_date}.ping"

mkdir -p "$(dirname "$log_file")"

while true; do
    current_time=$(date +"%Y-%m-%d %H:%M:%S")
    ping_result=$(ping -c 1 "$ip_address" | grep "seq=")
    echo "$current_time | $ping_result" >> "$log_file"
    sleep 1
done

添加执行权限

chmod +x ping_log.sh

命令行执行

./ping_log.sh 192.168.1.100

在日志文件夹 /var/log/ping 就可以找到对应的日志文件了,内容格式如下:

2025-03-10 09:56:39 | 64 bytes from 192.168.1.100: seq=0 ttl=64 time=0.000 ms
2025-03-10 09:56:40 | 64 bytes from 192.168.1.100: seq=0 ttl=64 time=4.000 ms
2025-03-10 09:56:41 | 64 bytes from 192.168.1.100: seq=0 ttl=64 time=4.000 ms
2025-03-10 09:56:42 | 64 bytes from 192.168.1.100: seq=0 ttl=64 time=4.000 ms
2025-03-10 09:56:43 | 64 bytes from 192.168.1.100: seq=0 ttl=64 time=0.000 ms


Pre: Centos7.x yum方式升级安装高版本GCC的方法

Next: 交叉编译 Valgrind 到 ARMv5 架构的 ARM926 CPU 的方法

14
Table of content