2023年6月1日 星期四

Ping

Ping
#!/bin/sh

dns_ips=("8.8.8.8" "8.8.4.4" "1.1.1.1" "1.1.1.2")

ping_times=10
sleep_sec=10
selected_ip=""
ping_result=""
ping_dns() {
    random_index=$((RANDOM % ${#dns_ips[@]}))
    selected_ip=${dns_ips[$random_index]}

    if ping -c $ping_times $selected_ip &> /dev/null; then
        echo "Ping to $selected_ip successful!"
        ping_result="y"
    else
        echo "Ping to $selected_ip failed!"
        log_failure $selected_ip
        ping_result="n"
    fi
}

failed_ip=""
log_failure() {
    failed_ip=$1
    echo "Ping to $failed_ip failed!" >> ping_log.txt
}

while true; do
    ping_dns
    echo "sleep $sleep_sec"
    sleep $sleep_sec
    if [ "$ping_result" == "y" ]; then
        echo "Ping to $selected_ip $ping_times times successful" >> ping_log.txt
    else
        dmesg | grep -i "usb 1-1.2" -A 5 > dmesg_log.txt
    fi
done

沒有留言:

張貼留言