# 定义ping函数 ping_ip() { local ip=$1 local delay_threshold=$2 local result local delay local status local datetime local output=""
for i in {1..5}; do if [[ "$OSTYPE" == "darwin"* ]]; then result=$(ping -c 1 -W 1000 $ip 2>&1) else result=$(ping -c 1 -W 1 $ip 2>&1) fi
datetime=$(date'+%Y-%m-%d %H:%M:%S')
if [[ $? -ne 0 ]]; then status="TIMEOUT" output+="$datetime$ip$status\n" else delay=$(awk -F'time=''/time=/{print $2+0}' <<< "$result") if (( $(awk "BEGIN {print ($delay > $delay_threshold)}") )); then status="EXCEEDED" output+="$datetime$ip$delay ms $status\n" fi fi done
if [[ -n "$output" ]]; then echo -e "$output" >> $LOG_FILE fi }