wireless 6.75 KB
Newer Older
James Rayner's avatar
James Rayner committed
1
2
3
4
# Uses wireless_tools, to check for association to a network.
# wep_check interface [timeout]
wep_check()
{
Jim Pryor's avatar
quotes!    
Jim Pryor committed
5
    local INTERFACE="$1" TIMEOUT="${2:-15}" timeout=0 bssid
6

Jim Pryor's avatar
quotes!    
Jim Pryor committed
7
8
    while [[ $timeout -lt "$TIMEOUT" ]]; do
        bssid=$(iwgetid "$INTERFACE" -ra)
Jim Pryor's avatar
Jim Pryor committed
9
        [[ -n "$bssid" && "$bssid" != "00:00:00:00:00:00" ]] && return 0
James Rayner's avatar
James Rayner committed
10
11
12
13
14
15
16
        sleep 1
        let timeout++
    done
    return 1
}

# Check if a particular network is within range
17
# find_essid interface essid connection (we treat ESSID as regexp when CONNECTION=wireless-dbus)
18
find_essid() {
19
    local INTERFACE="$1" ESSID="$2" CONNECTION="$3" RETRIES=10 try=0 res scanned
20
21
22
    while [[ "$try" -lt "$RETRIES" ]]; do
        sleep 0.5
        let try++
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        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"
                   )
38
        fi && {
39
40
41
42
43
44
                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
45
            echo "$found"       # JP: echo literal ESSID
James Rayner's avatar
James Rayner committed
46
47
            return 0 # network found
        fi
48
49
50
51
52
53
54
55
56
57
    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() {
58
    local INTERFACE="$1" ap=$(echo "$2" | tr 'abcdef' 'ABCDEF') RETRIES=10 try=0 res scanned
59
60
    while [[ "$try" -lt "$RETRIES" ]]; do
        sleep 0.5
James Rayner's avatar
James Rayner committed
61
        let try++
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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
James Rayner's avatar
James Rayner committed
78
    done
79
80
81
    if [[ "$scanned" -ne 1 ]]; then
        report_debug find_ap "unable to scan"
    fi
James Rayner's avatar
James Rayner committed
82
83
84
    return 1
}

85
# Return a filename containing a list of network APs and ESSIDs found (sorted by decreasing signal strength)
James Rayner's avatar
James Rayner committed
86
87
88
# list_networks interface
list_networks()
{
89
90
    local INTERFACE="$1" essids
    # temp file used, as keeping ESSID's with spaces in their name in arrays
James Rayner's avatar
James Rayner committed
91
    # is hard, obscure and kinda nasty. This is simpler and clearer.
92

93
    [[ -z "$INTERFACE" ]] && return 1
Jim Pryor's avatar
Jim Pryor committed
94
    essids=$(mktemp --tmpdir essid.XXXXXXXX)
James Rayner's avatar
James Rayner committed
95

96

97
98
99
100
101
102
103
104
105
    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 |
106
        cut -f1,5  > "$essids"
107
108
109
110
111
112
113

    # 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
114

115
    kill $(cat /var/run/wpa_supplicant.pid)
116

James Rayner's avatar
James Rayner committed
117
    # File of 0 length, ie. no ssid's.
Jim Pryor's avatar
Jim Pryor committed
118
119
    if [[ ! -s "$essids" ]]; then
        rm -f "$essids"
James Rayner's avatar
James Rayner committed
120
121
122
123
124
125
        return 1
    fi

    echo $essids
    return 0
}
126
127

list_networks() {
Jim Pryor's avatar
Jim Pryor committed
128
    wpa_supplicant_scan_info "$1" 1,5
129
130
131
132
133
134
135
136
137
138
}

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)

Jim Pryor's avatar
Jim Pryor committed
139
    wpa_supplicant -B -i"$INTERFACE" -Dwext -C/var/run/wpa_supplicant -P/var/run/wpa_supplicant.pid
140
141
142
143
144
145
146
147
    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 |
Jim Pryor's avatar
Jim Pryor committed
148
        cut -f"$fields"  > "$essids"
149
150
151
152
153
154
155
156

    # 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

Jim Pryor's avatar
Jim Pryor committed
157
    kill "$(cat /var/run/wpa_supplicant.pid)"
158
159
160
161
162
163
164
165
166
167
168

    # File of 0 length, ie. no ssid's.
    if [[ ! -s "$essids" ]]; then
        rm -f "$essids"
        return 1
    fi

    echo $essids
    return 0
}

169
set_rf_state() {
Jim Pryor's avatar
Jim Pryor committed
170
    local INTERFACE="$1" state="$2" PROFILE="$3"
171

Jim Pryor's avatar
Jim Pryor committed
172
173
    source "$IFACE_DIR/$INTERFACE"
    [[ -n "$PROFILE" ]] && source "$PROFILE_DIR/$PROFILE"  # profile overrides
174
    [[ $RFKILL == "hard" ]] && report_fail "Cannot set state on hardware rfkill switch"
Jim Pryor's avatar
Jim Pryor committed
175
176
    local path=$(get_rf_path "$INTERFACE" "$RFKILL_NAME")
    case "$state" in
177
        up)
Jim Pryor's avatar
Jim Pryor committed
178
            echo 1 > "$path/state"
179
180
        ;;
        down)
Jim Pryor's avatar
Jim Pryor committed
181
            echo 0 > "$path/state"
182
183
184
185
186
        ;;
    esac
}

get_rf_path() {
Jim Pryor's avatar
Jim Pryor committed
187
    local INTERFACE="$1" RFKILL_NAME="$2" path
188

Jim Pryor's avatar
Jim Pryor committed
189
    if [[ -n "$RFKILL_NAME" ]]; then
190
        for path in /sys/class/rfkill/*; do
Jim Pryor's avatar
Jim Pryor committed
191
192
            if [[ "$(cat "$path/name")" == "$RFKILL_NAME" ]]; then
                echo "$path"
193
194
195
196
197
                return 0
            fi
        done
        report_fail "no rfkill switch with name $RFKILL_NAME"
    else
Jim Pryor's avatar
Jim Pryor committed
198
199
200
        path="/sys/class/net/$INTERFACE/rfkill"
        if [[ -d "$path" ]]; then
            echo "$path"
201
202
203
204
205
206
207
            return 0
        fi
        report_fail "no rfkill switch available on interface $INTERFACE"
    fi
}

get_rf_state() {
Jim Pryor's avatar
Jim Pryor committed
208
209
210
211
212
    local INTERFACE="$1" PROFILE="$2" path state
    source "$IFACE_DIR/$INTERFACE"
    [[ -n "$PROFILE" ]] && source "$PROFILE_DIR/$PROFILE"  # profile overrides
    path=$(get_rf_path "$INTERFACE" "$RFKILL_NAME")
    state=$(cat "$path/state")
213

Jim Pryor's avatar
Jim Pryor committed
214
    case "$state" in
215
216
217
218
219
        0)
            echo "disabled";;
        1)
            echo "enabled";;
        *)
Jim Pryor's avatar
Jim Pryor committed
220
            echo "$state";;
221
222
223
    esac
}

Jim Pryor's avatar
Jim Pryor committed
224
# vim: ft=sh ts=4 et sw=4: