使用 glob 在 bash 中自动连接 wifi

使用 glob 在 bash 中自动连接 wifi

尝试申请这些理由在 ubuntu VM 盒上自动化 wifi 连接,但处理只是进入无限循环,因为我想我没有按应该的方式使用 glob,但找不到故障原因/位置:

我尝试使用以下命令运行它:

/etc/network/wlan-scan.sh wlan0

但在这里我看不出 bash 如何将全局变量与 /etc/network/interfaces 链接起来。所以我也尝试了:

ifup wlan0

/etc/network/wlan-scan.sh

#!/bin/bash
# https://help.ubuntu.com/community/WifiDocs/WiFiHowTo
# http://manpages.ubuntu.com/manpages/hardy/man5/interfaces.5.html
# http://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_mapping_stanza

# Turn on global for bash scripting
# see http://www.debian.org/doc/manuals/debian-reference/ch01.en.html#_shell_glob
echo  -n "Turn on global in bash ... : "
shopt -s dotglob
shopt -s extglob
shopt -s failglob
shopt -s globstar
shopt -s nocaseglob
shopt -s nullglob
echo "done"
shopt | grep glob

IFACE=$1
echo -n "Putting interface $IFACE up ... : "
ip link set $IFACE up
echo "done"

echo -n "Scanning $IFACE ... : "

SCAN=$( \
iwlist $IFACE scan 2>&1 | grep -v "^$IFACE" | grep -v "^$" | \
    sed -e "s/^\ *//" \
    -e "s/^Cell [0-9]\+ - /#/" \
    -e "s/^#Address: /#AP=/" \
    -e "s/^ESSID:/ESSID=/" \
    -e "s/^Encryption key:/ENC=/" \
-e "s/^Quality:\([0-9]\+\)\/.*$/QUALITY=\1/" \
-e "s/^.*Channel \([0-9]\+\).*$/CHANNEL=\1/" \
-e "s/^Mode:/MODE=/" \
    -e "s/Signal level.*dBm\ *//" \
        -e "s/^[^#].*:.*//" | \
    tr "\n#" "|\n" | \
    sed -e "s/[0-9]*Mb\/s//g" \
    -e "s/;//g" \
    -e "s/||*/|/g" \
    -e "s/\ *//g" \
-e "s/|[0-9]*|/|/g"
)
echo "done"


# need to put iface down before reconfiguring
echo -n "Putting interface $IFACE down ... : "
ip link set $IFACE down
echo "done"

# DEBUG
# set -x

which=""
while read glob scheme; do
    AP=""; QUALITY=""; CHANNEL=""; ESSID=""; MODE=""; ENC=""
    for i in $SCAN ; do
        # echo "browsing $i ..."
        for j in $(echo $i | sed -e "s/|/ /g" ) ; do
        echo "browsing $j ..."
            case $j in
                AP=*)
                    AP=${j#AP=}
                    ;;
                QUALITY=*)
                    QUALITY=${j#QUALITY=}
                    ;;
                CHANNEL=*)
                    CHANNEL=${j#CHANNEL=}
                    ;;
                ESSID=*)
                    ESSID=${j#ESSID=}
                    ;;
                MODE=*)
                    MODE=${j#MODE=}
                    ;;
                ENC=*)
                    ENC=${j#ENC=}
                    ;;
            esac
        done

        case "$AP,$ESSID,$ENC,$MODE,$CHANNEL" in
            $glob)
                echo $scheme
                exit 0
                ;;
        esac
    done
done

# DEBUG
# set +x

# Turn back off global for bash scripting
echo -n "Turn back off global for bash ... : "
shopt -u dotglob
shopt -u extglob
shopt -u failglob
shopt -u globstar
shopt -u nocaseglob
shopt -u nullglob
echo "done"
shopt | grep glob

exit 0

/etc/网络/接口

mapping wlan0
        script /etc/network/wlan-scan.sh
        # Accesspoint,"ESSID",Encryption,Mode,Channel
        map AA:BB:CC:DD:EE:FF,"my_wifi_1",on,Managed,11 box_one
        map AA:BB:CC:DD:EE:AA,"my_wifi_2",on,Managed,10 box_two

iface box_one inet dhcp
        wireless-essid my_wifi_1
        wireless-mode managed
        wireless-enc <HEX-WEP1>

iface box_two inet dhcp
        wireless-essid my_wifi_2
        wireless-mode managed
        wireless-enc <HEX-WEP2>

答案1

实际上,只需进行微小的修改即可...

只需从脚本中删除所有“echo”(echo $scheme 除外),然后运行

ifup wlan0=box_one

这里是一些有帮助的文档。

相关内容