#! /bin/bash # Originally contributed by Neuro: http://bbs.archlinux.org/viewtopic.php?pid=278148#p278148 . /usr/lib/network/network . /usr/lib/network/wireless # wifi_auto # autoconnect wireless interface # $1 - wireless interface wifi_auto() { local interface="$1" report_try "Scanning for networks" set_interface up "$interface" # uses iproute methods---is it there any value to providing option to use ifconfig? networks="$(list_networks $interface)" if [[ -z $networks ]]; then set_interface forcedown "$interface" exit_fail "- No networks available." fi # Loop through all the found essid's, then find a matching profile. local found_profile found_essid # JP: add ability to use AP instead of ESSID # JP: also, make ESSIDs in wireless-dbus CONNECTIONS a regexp instead of a literal while read ap essid; do while read network; do ( unset CONNECTION INTERFACE AP load_profile "$network" case "$CONNECTION" in wireless-old|wireless|wireless-dbus) if [[ "$interface" = "$INTERFACE" ]]; then if [[ "$ap" == "$AP" ]]; then exit 2 elif [[ -z "$found_profile" ]]; then if [[ "$CONNECTION" == wireless-dbus ]]; then if expr match "$essid" "^$ESSID\$" 1>/dev/null; then exit 1 fi elif [[ "$essid" == "$ESSID" ]]; then exit 1 fi fi fi ;; esac exit 0 ) case $? in 2) found_profile="$network" found_essid="$essid" break 2;; 1) found_profile="$network" found_essid="$essid" ;; esac done < <(list_profiles) # avoid subshell we'd get by piping list_profiles to while read done < "$networks" # avoid subshell; list_networks returns name of a tmp file rm -f "$networks" # shouldn't we delete the tmp file? if [[ -n "$found_profile" ]]; then report_success if profile_up "$found_profile" "$found_essid"; then # JP: now we pass literal essid to profile_up as $2 exit 0 else set_interface forcedown "$interface" exit_fail "Couldn't connect profile $found_profile." fi else set_interface forcedown "$interface" exit_fail "No profiles matched the local networks." fi } if [[ $(id -u) -ne 0 ]]; then exit_stderr "This script needs to be run with root privileges" fi if [[ -z "$1" ]]; then exit_stderr "Please supply an interface to connect" fi wifi_auto "$1"