Created
September 24, 2014 03:15
-
-
Save elecnix/182fa522da5dc7389975 to your computer and use it in GitHub Desktop.
Parse iw scan output
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
# iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f scan.awk | |
$1 == "BSS" { | |
MAC = $2 | |
print $2 | |
e = wifi[MAC] | |
e["enc"] = "Open" | |
} | |
$1 == "SSID:" { | |
e = wifi[MAC] | |
e["SSID"] = $2 | |
} | |
$1 == "freq:" { | |
e = wifi[MAC] | |
e["freq"] = $NF | |
} | |
$1 == "signal:" { | |
e = wifi[MAC] | |
e["sig"] = $2 " " $3 | |
} | |
$1 == "WPA:" { | |
e = wifi[MAC] | |
e["enc"] = "WPA" | |
} | |
$1 == "WEP:" { | |
e = wifi[MAC] | |
e["enc"] = "WEP" | |
} | |
END { | |
printf "%s\t\t%s\t%s\t\t%s\n","SSID","Frequency","Signal","Encryption" | |
for (w in wifi) { | |
e = wifi[w] | |
printf "%s\t\t%s\t\t%s\t%s\n",e["SSID"],e["freq"],e["sig"],e["enc"] | |
} | |
} |
Thanks @Djey1301 for your BusyBox version!
Most scripts fail to correctly parse the SSID: it's pretty common to have WiFi names with spaces, e.g. "Guest Network", which is a separator for awk.
Solution: e["SSID"] = substr($0, index($0,$2));
@zehnm ur welcome :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not compatible with BusyBox
I propose this version :
iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f scan.awk
BEGIN {
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n","MAC","SSID","freq","signal","sig%","WPA","WPA2","WEP","TKIP","CCMP");
}
NF > 0{$2 ~ /^[a-z0-9:]{17}$ / ) {
if ($1 == "BSS") {
if(
if( e["MAC"] ){
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%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"] = $2;
}
if ($1 == "freq:") {
e["freq"] = $NF;
}
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("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",e["MAC"],e["SSID"],e["freq"],e["sig"],e["sig%"],e["WPA"],e["WPA2"],e["WEP"],e["TKIP"],e["CCMP"]);
}