# Uses wireless_tools, to check for association to a network. # wep_check interface [timeout] wep_check() { local INTERFACE="$1" TIMEOUT="${2:-15}" timeout=0 bssid while [[ $timeout -lt "$TIMEOUT" ]]; do bssid=$(iwgetid "$INTERFACE" -ra) [[ -n "$bssid" && "$bssid" != "00:00:00:00:00:00" ]] && return 0 sleep 1 let timeout++ done return 1 } # Check if a particular network is within range # find_essid interface essid connection (we treat ESSID as regexp when CONNECTION=wireless-dbus) find_essid() { local INTERFACE="$1" ESSID="$2" CONNECTION="$3" RETRIES=10 try=0 res scanned while [[ "$try" -lt "$RETRIES" ]]; do sleep 0.5 let try++ if [[ "$CONNECTION" == wireless-dbus ]]; then # JP: ESSID is a regexp found=$( res=$(iwlist "$INTERFACE" scan 2>/dev/null) [[ -z "$res" ]] && exit 1 # if results were non-null, process them and exit 0 echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | egrep -xm1 "$ESSID" ) else found=$( res=$(iwlist "$INTERFACE" scan 2>/dev/null) [[ -z "$res" ]] && exit 1 # if results were non-null, process them and exit 0 echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | fgrep -xm1 "$ESSID" ) fi && { scanned=1 report_debug find_essid "\"$found\"" # we only bother with at most 5 successful scans if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi } if [[ -n "$found" ]]; then echo "$found" # JP: echo literal ESSID return 0 # network found fi done if [[ "$scanned" -ne 1 ]]; then report_debug find_essid "unable to scan" fi return 1 } # Check if a particular network is within range # find_ap interface ap find_ap() { local INTERFACE="$1" ap=$(echo "$2" | tr 'abcdef' 'ABCDEF') RETRIES=10 try=0 res scanned while [[ "$try" -lt "$RETRIES" ]]; do sleep 0.5 let try++ found=$( res=$(iwlist "$INTERFACE" scan 2> /dev/null) [[ -z "$res" ]] && exit 1 # if results were non-null, process them and exit 0 echo "$res" | sed -nr '/^\s+Cell .. - Address: ([[:xdigit:]:]+)$/ { s//\1/; N; s/(.*)\n\s+ESSID:"([^"]*)"$/\1\t\2/p }' \ | egrep -m1 "^$ap\t" ) && { scanned=1 report_debug find_ap "\"$found\"" # we only bother with at most 5 successful scans if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi } if [[ -n "$found" ]]; then echo "$found" | cut -f2 # JP: echo literal ESSID return 0 fi done if [[ "$scanned" -ne 1 ]]; then report_debug find_ap "unable to scan" fi return 1 } # Return a filename containing a list of network APs and ESSIDs found (sorted by decreasing signal strength) # list_networks interface list_networks() { local INTERFACE="$1" essids # temp file used, as keeping ESSID's with spaces in their name in arrays # is hard, obscure and kinda nasty. This is simpler and clearer. [[ -z "$INTERFACE" ]] && return 1 essids=$(mktemp --tmpdir essid.XXXXXXXX) wpa_supplicant -B -i$INTERFACE -Dwext -C/var/run/wpa_supplicant -P/var/run/wpa_supplicant.pid wpa_cli -i "$INTERFACE" scan &> /dev/null sleep 2.5 wpa_cli -i "$INTERFACE" scan_results | grep -v "^Selected" | grep -v "^bssid" | sort -rn -k3 | sort -u -k5 | sort -rn -k3 | cut -f1,5 > "$essids" # Fields are tab delimited # Remove extraneous output from wpa_cli # Sort by strength # Remove duplicates # Re-sort by strength as the removal disorders the list # Cut to the AP/essid fields only kill $(cat /var/run/wpa_supplicant.pid) # File of 0 length, ie. no ssid's. if [[ ! -s "$essids" ]]; then rm -f "$essids" return 1 fi echo $essids return 0 } list_networks() { wpa_supplicant_scan_info $1 1,5 } wpa_supplicant_scan_info() { local INTERFACE="$1" fields="$2" essids # temp file used, as keeping ESSID's with spaces in their name in arrays # is hard, obscure and kinda nasty. This is simpler and clearer. [[ -z "$INTERFACE" ]] && return 1 essids=$(mktemp --tmpdir essid.XXXXXXXX) wpa_supplicant -B -i$INTERFACE -Dwext -C/var/run/wpa_supplicant -P/var/run/wpa_supplicant.pid wpa_cli -i "$INTERFACE" scan &> /dev/null sleep 2.5 wpa_cli -i "$INTERFACE" scan_results | grep -v "^Selected" | grep -v "^bssid" | sort -rn -k3 | sort -u -k5 | sort -rn -k3 | cut -f$fields > "$essids" # Fields are tab delimited # Remove extraneous output from wpa_cli # Sort by strength # Remove duplicates # Re-sort by strength as the removal disorders the list # Cut to the AP/essid fields only kill $(cat /var/run/wpa_supplicant.pid) # File of 0 length, ie. no ssid's. if [[ ! -s "$essids" ]]; then rm -f "$essids" return 1 fi echo $essids return 0 } set_rf_state() { local INTERFACE=$1 state=$2 PROFILE=$3 source $IFACE_DIR/$INTERFACE [[ $PROFILE ]] && source $PROFILE_DIR/$PROFILE # profile overrides [[ $RFKILL == "hard" ]] && report_fail "Cannot set state on hardware rfkill switch" path=$(get_rf_path $INTERFACE $RFKILL_NAME) case $state in up) echo 1 > $path/state ;; down) echo 0 > $path/state ;; esac } get_rf_path() { local INTERFACE=$1 RFKILL_NAME=$2 if [[ $RFKILL_NAME ]]; then for path in /sys/class/rfkill/*; do if [[ "$(cat $path/name)" == $RFKILL_NAME ]]; then echo $path return 0 fi done report_fail "no rfkill switch with name $RFKILL_NAME" else path=/sys/class/net/$INTERFACE/rfkill if [[ -d $path ]]; then echo $path return 0 fi report_fail "no rfkill switch available on interface $INTERFACE" fi } get_rf_state() { local INTERFACE=$1 PROFILE=$2 source $IFACE_DIR/$INTERFACE [[ $PROFILE ]] && source $PROFILE_DIR/$PROFILE # profile overrides path=$(get_rf_path $INTERFACE $RFKILL_NAME) state=$(cat $path/state) case $state in 0) echo "disabled";; 1) echo "enabled";; *) echo $state;; esac } # vim: set ts=4 et sw=4 ft=sh: