Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Trying to apply these rationals on an ubuntu VM box to automate wifi connections but processing just into an infinite loop as I guess I'm not using glob as it should but can't find why/where the fault(s) :

I tried to run this using :

/etc/network/wlan-scan.sh wlan0

but here I see no point how bash would link globals with /etc/network/interfaces. So I tried also like :

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/network/interfaces

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>
share|improve this question

1 Answer 1

Actually this works with minor changes ...

Just need to remove all "echo"s from the script (except echo $scheme), and run

ifup wlan0=box_one

Here is some documentation that helped.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.