Skip to content

Instantly share code, notes, and snippets.

@elecnix
Created September 24, 2014 03:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save elecnix/182fa522da5dc7389975 to your computer and use it in GitHub Desktop.
Save elecnix/182fa522da5dc7389975 to your computer and use it in GitHub Desktop.
Parse iw scan output
# 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"]
}
}
@sidesteps
Copy link

sidesteps commented Aug 8, 2016

awk: scan.awk:5: (FILENAME=- FNR=1) fatal: attempt to use scalar `e' as an array

@Djey1301
Copy link

Djey1301 commented Sep 29, 2016

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{
if ($1 == "BSS") {
if( $2 ~ /^[a-z0-9:]{17}$/ ) {
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"]);
}

@zehnm
Copy link

zehnm commented Oct 13, 2019

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));

@Djey1301
Copy link

@zehnm ur welcome :)

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