Created
January 4, 2021 20:56
-
-
Save pschmitt/2a03371e82306c8180171480287a56eb to your computer and use it in GitHub Desktop.
wifi-scan.zsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wifi-scan() { | |
setopt localoptions pipefail errreturn | |
local device="$(wifnames | tail -1)" | |
local raw | |
zparseopts -D -K -E \ | |
r=raw -raw=raw | |
if [[ -z "$device" ]] | |
then | |
echo_err "Unable to determine wifi device" | |
return 1 | |
fi | |
echo_debug "Scanning using $device" | |
sudo ip link set dev "$device" up | |
local res="$(sudo iw dev "$device" scan)" | |
local jres | |
local LC_NUMERIC=C | |
jres="$(sed -e 's#(on w.*# (on w#g' <<< "$res" | awk ' | |
BEGIN { | |
printf("[\n") | |
} | |
NF > 0 { | |
if ($1 == "BSS") { | |
if ($2 ~ /^[a-z0-9:]{17}$/) { | |
if (e["MAC"]) { | |
printf("{\"mac\":\"%s\",\"ssid\":\"%s\",\"freq\":\"%1.1f\",\"sig\":\"%s\",\"sigpct\":\"%.0d\",\"wpa\":\"%s\",\"wpa2\":\"%s\",\"wep\":\"%s\",\"tkip\":\"%s\",\"ccmp\":\"%s\"},\n", | |
e["MAC"], e["SSID"], e["freq"], e["sig"], e["sig%"], e["WPA"], e["WPA2"], e["WEP"], e["TKIP"], e["CCMP"]); | |
} | |
e["MAC"] = $2; | |
e["WPA"] = "n"; | |
e["WPA2"] = "n"; | |
e["WEP"] = "n"; | |
e["TKIP"] = "n"; | |
e["CCMP"] = "n"; | |
} | |
} | |
if ($1 == "SSID:") { | |
e["SSID"] = substr($0, index($0,$2)); | |
} | |
if ($1 == "freq:") { | |
e["freq"] = $NF / 1000; | |
} | |
if ($1 == "signal:") { | |
e["sig"] = $2 " " $3; | |
e["sig%"] = (60 - ((-$2) - 40)) * 100 / 60; | |
} | |
if ($1 == "WPA:") { | |
e["WPA"] = "y"; | |
} | |
if ($1 == "RSN:") { | |
e["WPA2"] = "y"; | |
} | |
if ($1 == "WEP:") { | |
e["WEP"] = "y"; | |
} | |
if ($4 == "CCMP" || $5 == "CCMP") { | |
e["CCMP"] = "y"; | |
} | |
if ($4 == "TKIP" || $5 == "TKIP") { | |
e["TKIP"] = "y"; | |
} | |
} | |
END { | |
printf("{\"mac\":\"%s\",\"ssid\":\"%s\",\"freq\":\"%.1f\",\"sig\":\"%s\",\"sigpct\":\"%.0d\",\"wpa\":\"%s\",\"wpa2\":\"%s\",\"wep\":\"%s\",\"tkip\":\"%s\",\"ccmp\":\"%s\"}\n", | |
e["MAC"], e["SSID"], e["freq"], e["sig"], e["sig%"], e["WPA"], e["WPA2"], e["WEP"], e["TKIP"], e["CCMP"]); | |
printf("]\n") | |
}')" | |
# Sanitize JSON | |
# FIXME Why do we need to echo here for tr to do its jobs?! | |
jres="$(echo "$jres" | tr -d '\000' | tr -d '\t')" | |
# Fix frequency display | |
jres=$(jq '.[] |= | |
if ((.freq | tonumber) < 2.4 or (.freq | tonumber) >=6.0) then (.freq = (.freq | tostring)) | |
elif ((.freq | tonumber) > 5) then (.freq = "5.0") | |
elif ((.freq | tonumber)> 2.0) then (.freq = "2.4") | |
else . end' <<< "$jres") | |
# Fix signal percentage (it should never exceed 100%) | |
jres=$(jq '.[] |= | |
if ((.sigpct | tonumber) > 100) then (.sigpct = "100") else . end' \ | |
<<< "$jres") | |
if [[ -n "$raw" ]] | |
then | |
jq <<< "$jres" | |
else | |
jq '. |= sort_by(.sigpct | tonumber)' <<< "$jres" | \ | |
jq -r ' | |
def colors: | |
{ | |
"black": "\u001b[30m", | |
"red": "\u001b[31m", | |
"green": "\u001b[32m", | |
"yellow": "\u001b[33m", | |
"blue": "\u001b[34m", | |
"magenta": "\u001b[35m", | |
"cyan": "\u001b[36m", | |
"white": "\u001b[37m" | |
}; | |
def styles: | |
{ | |
"strikethrough": "\u001b[9m", | |
"reset": "\u001b[0m" | |
}; | |
.[] | colors.blue + (if (.ssid == "") then (styles.strikethrough + "[*NONE*] (hidden?)" + styles.reset) else .ssid end) + styles.reset + "\t" + colors.magenta + .mac + "\t" + colors.yellow + .freq + styles.reset + "\t" + colors.green + .sigpct + "%" + styles.reset' | \ | |
column -t -s ' ' | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: