使bash脚本检查连接并在必要时更改连接.帮帮我改进吗?

cyp*_*nks 2 bash connectivity

我的连接很不稳定,但我有一个备份.我做了一些bash脚本来检查连接并改变连接,如果现有的已经死了.请帮我改进它们.

这些脚本几乎可以工作,除了没有等待足够长的时间来接收IP(它直到循环中的下一步循环太快).开始:

#!/bin/bash
# Invoke this script with paths to your connection specific scripts, for example
# ./gotnet.sh ./connection.sh ./connection2.sh

until [ -z "$1" ]  # Try different connections until we are online...
    do
    if eval "ping -c 1 google.com"
    then
        echo "we are online!" && break
    else
    $1     # Runs (next) connection-script.
    echo
    fi
     shift
   done

   echo               # Extra line feed.
exit 0
Run Code Online (Sandbox Code Playgroud)

以下是slave脚本的示例:

#!/bin/bash
ifconfig wlan0 down
ifconfig wlan0 up
iwconfig wlan0 key 1234567890
iwconfig wlan0 essid example
sleep 1
dhclient -1 -nw wlan0
sleep 3
exit 0
Run Code Online (Sandbox Code Playgroud)

mus*_*XXX 6

这是一种方法:

#!/bin/bash
while true; do
        if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping exits nonzero...
                ./connection_script1.sh #run the first script
                sleep 10     #give it a few seconds to complete
        fi
        if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping *still* exits nonzero...
                ./connection_script2.sh #run the second script
                sleep 10     #give it a few seconds to complete
        fi
        sleep 300 #check again in five minutes
done
Run Code Online (Sandbox Code Playgroud)

根据您的喜好调整睡眠时间和ping计数.此脚本永远不会退出,因此您很可能希望使用以下命令运行它:

./connection_daemon.sh 2>&1>/dev/null&disown