diff options
author | James Rayner <james@archlinux.org> | 2009-08-10 12:43:58 +0200 |
---|---|---|
committer | James Rayner <james@archlinux.org> | 2009-08-10 12:43:58 +0200 |
commit | 78e2fa6621750e4f2a108f90598d9a7e798b46b3 (patch) | |
tree | 525873acd2adacd093bc1695fb080021944c26b2 /src/wireless | |
parent | 9a3078d8cec470b3820c5597d5c8819d642e558d (diff) | |
parent | 1400a8362753016dacc699679e8b9daebd3c8513 (diff) | |
download | netctl-78e2fa6621750e4f2a108f90598d9a7e798b46b3.tar.gz netctl-78e2fa6621750e4f2a108f90598d9a7e798b46b3.tar.xz |
merge changes since revert
Diffstat (limited to 'src/wireless')
-rw-r--r-- | src/wireless | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/wireless b/src/wireless new file mode 100644 index 0000000..f45028e --- /dev/null +++ b/src/wireless @@ -0,0 +1,100 @@ +# 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 -ne $TIMEOUT ]]; do + bssid=`iwgetid $INTERFACE -ra` + [[ ! "$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 +find_essid() { + local INTERFACE="$1" ESSID="$2" RETRIES=20 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/^\s+ESSID:"([^"]*)"$/\1/p' | fgrep -xm1 "$ESSID" + ) && { + 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 + 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=20 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 ESSID's found. +# list_networks interface +list_networks() +{ + # 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 "$1" ]] && return 1 + essids=$(mktemp /tmp/essid.XXXXX) + + let try=0; + RETRIES=6; + while [[ $try -ne $RETRIES ]]; do + iwlist $1 scan 2> /dev/null|grep ESSID|sed 's/.*ESSID:"\([^"]\+\)".*/\1/' > $essids + sleep 0.5; let try++ + done + sort -u $essids -o $essids + + # File of 0 length, ie. no ssid's. + if [[ ! -s $essids ]]; then + return 1 + fi + + echo $essids + return 0 +} +# vim: set ts=4 et sw=4 ft=sh: |