如何旋转WiFi连接?

我有2个WiFI连接可用。 有时,WiFi网络断开连接,当发生这种情况时,我希望系统自动使用其他连接。

我怎么能在Ubuntu中这样做?

这是一个SMOP。 我看着upstartdbusudev ,但没有一个适合。 我决定NMP(NMP:不是我的问题;留给学生的练习)如何启动它(见下文)并让我们一直运行(尽管~/.config/autostart可能会这样做)。 我还NMP了如何重新启动网络,将它留在配置文件中的单行命令。

 #!/bin/bash # Monitor for the net going down, Do The Next Thing (from a config file) # to bring the net up. Implicit assumpition that The Next Thing fixes it # may be a problem. # Walt Sullivan # Note: places where you may need to adjust things to match # your environment/taste are marked "#Adjust" # determine my name me=$0 me=${me##*/} # my variables debug=0 # set via --debug verbose=0 # set via --verbose original="" # set the  original_update=0 # time  was last modified config="/var/tmp/${me}.$$.config" # my writable copy of config result="" # temporary use # $ dpkg -S $(type -p nm-online) # network-manager: /usr/bin/nm-online # $ dpkg -S $(type -p ip) # iproute2: /sbin/ip # man ip-link;man ip-monitor;man ip-address;man 7 regex # -h or --help or something's wrong in here help () { echo "${me} [-h|--help] [-v|--verbose]  " >&2 echo "" >&2 echo "Monitor the wireless network, and when it goes down, Do The" >&2 echo "Next Thing (as specified by the ), to bring" >&2 echo "the wireless net up." >&2 echo "" >&2 echo "The  contains #comments, blank lines, AND" >&2 echo "single line commands, of your choice, to correct the" >&2 echo "wireless network down condition. The first command in the" >&2 echo " will be executed the first time the net goes" >&2 echo "down (or if the net is down when ${me} begins), the second" >&2 echo "command will be executed the next time the net goes down," >&2 echo "and so forth, wrapping around at the end. The number of" >&2 echo "single line commands in the  is unlimited." >&2 exit 2 } function flip () { # Return the first non-blank, non #comment line, # and move that line (and all preceeding blank and #comment # lines) to the end of (our copy of) the config file. # # ed pattern includes "/", but not "~" or "." #Adjust the ed pattern in both places and in countconfiglines ed --quiet "$config" <&2 exit 4 else /bin/cp --force "$original" "$config" original_update="$new_update" fi fi } function netstate () { # Return network state as "UP" or "DOWN" #Adjust how you decide net is UP/DOWN ip link show | egrep -q 'UP,LOWER_UP.* state UP' if [[ $? -eq 0 ]] ; then echo "UP" else echo "DOWN" fi } # Execution begins here # parse the args with getopt, adapted from # /usr/share/doc/util-linux/examples/getopt-parse.bash TEMP=`getopt -o dhv --long debug,help,verbose \ -n "${me}" -- "$@"` if [[ $? != 0 ]] ; then echo "${me} --help for help." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! eval set -- "$TEMP" while true ; do case "$1" in -d|--debug) debug=1; shift;; -h|--help) help; shift;; -v|--verbose) verbose=1; shift;; --) shift; break;; *) echo "Internal error! ${me} --help for help";exit 1;; esac done # Did we get the ? original="$1" shift [[ -z "$original" ]] && \ (echo "Missing config file ${me} -h for help" >&2 ; exit 1) # If there are more parameters, confusion exists in the mind of the caller [[ "$#" -ne 0 ]] && help [[ -r "$original" ]] || (echo "${me}:Cannot read $original" >&2 exit 2) if [[ $(countconfiglines "$original" ) -eq 0 ]] ; then echo "${me}:Invalid configuration in $original" >&2 exit 4 else # watch for changes, record "%Y time of last modification, # seconds since Epoch" original_update="$(/usr/bin/stat --format='%Y' $original )" # make a writeable copy for our use, and clean it up at the end # unless $debug [[ $debug -ne 0 ]] || trap "/bin/rm -f $config" EXIT /bin/cp --force "$original" "$config" fi # if the net is down, Do The Next Thing right away #Adjust: how you decide the net is up or down? [[ "$(netstate)" = "DOWN" ]] && \ ( result="$(flip)"; [[ $verbose ]] && echo "$(date):$result" >&2; \ eval "$result" ) # Wait for the net to go down, then Do The Next Thing #Adjust find a better way to watch for net down ip monitor address | \ egrep --line-buffered \ '^Deleted [[:digit:]]+: [[:alnum:]]+[[:space:]]+inet[[:space:]].* scope global ' | \ while read line ; do up-to-date result="$(flip)" [[ $verbose -eq 1 ]] && echo "$(date):$result" >&2 # Here is where "The Next Thing" is Done eval "$result" done # we never exit the while loop, until the world ends. exit 0 

或者, git clone https://github.com/waltinator/net-o-matic.git来获得上述内容。