wifi自动重连

wifi自动重连

通常 wifi 会在断开后重新连接,但有时会显示 wifi 登录屏幕已填满,等待按连接后,将在重复的 ssid 后以 #2 形成新连接。

有时我只是取消勾选/勾选“启用网络”。其他时候,它不会重新连接。

我尝试编写一个脚本,但即使在达到 30 计数的最大限制之前没有任何 badping,它也会继续累积和计数。

while true; 
do
    if ! [ "$(ping -c 1 google.com)" ]; then
        echo "no ping,will reset" 

    #counter of bad pings here
    count=1             
    while [ $count -lt 30 ]
        do
            count=`expr $count + 1`
            echo "$count"
            # insert here: retest for good ping
            sleep 1
        done


        nmcli networking off 
        sleep 5 
        nmcli networking on


        #sleep for 15sec wait wifi on ssid search        
        secs=$((1 * 15))
        while [ $secs -gt 0 ]; do
        echo -ne "$secs\033[0K\r"wifi reactivate in- 
        sleep 1
         : $((secs--))
        done


        if ! [ "$(ping -c 1 google.com)" ]; then
             echo "still offline for $count x @ $(date)" 

         else 
         count=0
             #echo "reconnected at $(date)" 
         echo "ON"
        fi
        else

        echo "ONLINE"
        sleep 1
    clear
    fi
done

答案1

我认为这更接近你想要的,但绝对可以进一步改进:

while :; do
    if ! ping -c1 google.com >/dev/null 2>&1; then
        echo "no ping,will reset" 
        #counter of bad pings here
        count=1             
        while [ "$count" -lt 30 ]; do
            echo "$count"
            # insert here: retest for good ping
            nmcli networking off 
            sleep 5 
            nmcli networking on
            if ! ping -c1 google.com >/dev/null 2>&1; then
                echo "still offline for $count x @ $(date)" 
            else 
                #echo "reconnected at $(date)" 
                echo "ON"
                break
            fi
            ((count++))
            sleep 1
        done
    else
        echo "ONLINE"
        sleep 1
        clear
    fi
done

相关内容