Skip to content

Instantly share code, notes, and snippets.

@pschmitt
Created January 4, 2021 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pschmitt/2a03371e82306c8180171480287a56eb to your computer and use it in GitHub Desktop.
Save pschmitt/2a03371e82306c8180171480287a56eb to your computer and use it in GitHub Desktop.
wifi-scan.zsh
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
}
@pschmitt
Copy link
Author

pschmitt commented Jan 4, 2021

Sample output:

$ wifi-scan
🐛 Scanning using wlan0
UPC956E146          54:67:51:2c:3d:0a  2.4  30%
Vodafone Hotspot    36:2c:94:34:3b:95  2.4  31%
Medusa_13           34:2c:c4:34:3b:95  2.4  33%
Gast_Medusa_13      36:2c:b4:34:3b:95  2.4  35%
UPC562CD8D          54:67:51:08:ce:3b  5.0  35%
Vodafone-3101       d4:3f:cb:89:a0:89  5.0  35%
BeMaFRA             98:9b:cb:71:86:ed  5.0  35%
Vodafone Hotspot    56:67:11:08:ce:63  2.4  40%
BeMaFRA             98:9b:cb:71:86:ee  2.4  41%
Hoeheitsgebiet      ac:22:05:db:4d:22  5.0  43%
o2-WLAN34           a8:d3:f7:96:10:69  2.4  48%
Vodafone Hotspot    92:5c:14:db:21:48  2.4  51%
UPC614F5E5          90:5c:44:db:21:48  2.4  53%
Hoeheitsgebiet      ac:22:05:db:4d:5b  2.4  56%
[*NONE*] (hidden?)  6a:9a:87:e7:90:77  2.4  58%
Vodafone Hotspot    ae:22:15:db:4d:5b  2.4  60%
Vodafone Hotspot    92:5c:14:d1:34:2f  2.4  66%
UPC5144FAF          90:5c:44:d1:34:2f  2.4  68%
UPC5144FAF          90:5c:44:d1:34:20  5.0  71%
Vodafone Hotspot    ae:22:15:e6:ff:41  2.4  83%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment