function log()
{
if [[ $# -lt 2 ]]; then
return
fi
local file=$1
shift 1
echo "$(date '+%F %T') [INFO] $*" >> $file
}
ip=$1
while :
do
l=`ping -c 1 $ip | grep :`
logname="$ip.$(date '+%F')"
log $logname $l
sleep 1
done
#启动
ps -ef | grep "ping.sh" | awk -F " " '{print $2}' | xargs kill
nohup ./ping.sh 127.0.0.1 &
它会每秒ping一次,并将带有时间信息一并写入到ip.日期格式的日志文件中
如下:
2022-10-27 09:32:14 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
2022-10-27 09:32:15 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
2022-10-27 09:32:16 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
2022-10-27 09:32:17 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
2022-10-27 09:32:18 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
2022-10-27 09:32:19 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.022 ms
2022-10-27 09:32:20 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
2022-10-27 09:32:21 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
2022-10-27 09:32:22 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
2022-10-27 09:32:23 [INFO] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.022 ms