]> git.corax.cc Git - toolbox/commitdiff
include/iface: Add function for scanning for wireless network
authorMatthias Kruk <m@m10k.eu>
Sat, 27 Mar 2021 03:10:05 +0000 (12:10 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 27 Mar 2021 03:10:05 +0000 (12:10 +0900)
The output from `iwlist scan' is rather hard to look at and bothersome
to use in scripts. This commit adds a function that parses the output
into a more machine-friendly format.

include/iface.sh

index a71ae2964218be751df4cd58face8ab6ada6c6fb..47df89a59fd0672aaf29747ea66c3540214c9a9c 100644 (file)
@@ -26,7 +26,7 @@ iface_get_state() {
                return 1
        fi
 
-       if ! state=$(echo "$state" | grep -oP "state[ ]+\K[^ ]+"); then
+       if ! state=$(echo "$state" | grep -oP 'state[ ]+\K[^ ]+'); then
                return 1
        fi
 
@@ -76,8 +76,8 @@ iface_set_state() {
 }
 
 iface_get_address() {
-        local iface
-        local proto
+       local iface
+       local proto
 
         local addr_all
 
@@ -88,7 +88,7 @@ iface_get_address() {
                 return 1
         fi
 
-        if ! echo "$addr_all" | grep -oP "$proto[ \t]+\K[^ ]+"; then
+        if ! echo "$addr_all" | grep -oP "${proto}[ \\t]+\\K[^ ]+"; then
                 return 1
         fi
 
@@ -105,9 +105,51 @@ iface_get_essid() {
                 return 1
         fi
 
-        if ! echo "$addr_all" | grep -oP "$iface.*ESSID:\"\K[^\"]+"; then
+        if ! echo "$addr_all" | grep -oP "$iface.*ESSID:\"\\K[^\"]+"; then
                 return 1
         fi
 
         return 0
 }
+
+_iface_parse_iwlist() {
+       local regex_ssid='Cell [0-9]+ - Address: ([0-9A-Fa-f:]+)'
+       local regex_signal='Quality=([0-9/]+)'
+       local regex_essid='ESSID:"(.*)"'
+
+        local line
+        local ssid
+        local essid
+        local strength
+
+        while read -r line; do
+                if [[ "$line" =~ $regex_ssid ]]; then
+                        #start of a new network
+                        ssid="${BASH_REMATCH[1]}"
+                        essid=""
+                        strength=""
+                elif [[ "$line" =~ $regex_signal ]]; then
+                        strength="${BASH_REMATCH[1]}"
+                elif [[ "$line" =~ $regex_essid ]]; then
+                        essid="${BASH_REMATCH[1]}"
+                fi
+
+                if [ -n "$ssid" ] && [ -n "$essid" ] && [ -n "$strength" ]; then
+                        echo "$ssid $strength $essid"
+                fi
+        done
+}
+
+iface_scan() {
+       local iface
+       local raw
+
+       iface="$1"
+
+       if ! raw=$(iwlist "$iface" scan 2>&1); then
+               return 1
+       fi
+
+       echo "$raw" | _iface_parse_iwlist
+       return 0
+}