2021年10月12日 星期二

Parsing iw wlan0 scan output

 

Here is my final solution based of Sudo_O answer:

$1 == "BSS" {
    MAC = $2
    wifi[MAC]["enc"] = "Open"
}
$1 == "SSID:" {
    wifi[MAC]["SSID"] = $2
}
$1 == "freq:" {
    wifi[MAC]["freq"] = $NF
}
$1 == "signal:" {
    wifi[MAC]["sig"] = $2 " " $3
}
$1 == "WPA:" {
    wifi[MAC]["enc"] = "WPA"
}
$1 == "WEP:" {
    wifi[MAC]["enc"] = "WEP"
}
END {
    printf "%s\t\t%s\t%s\t\t%s\n","SSID","Frequency","Signal","Encryption"

    for (w in wifi) {
        printf "%s\t\t%s\t\t%s\t%s\n",wifi[w]["SSID"],wifi[w]["freq"],wifi[w]["sig"],wifi[w]["enc"]
    }
}'

Output:

$ sudo iw wlan0 scan | awk -f scan.awk
SSID        Frequency   Signal      Encryption
netti       2437        -31.00 dBm  Open
Koti783     2437        -84.00 dBm  WPA
WLAN-AP     2462        -85.00 dBm  WPA

資料來源: https://stackoverflow.com/questions/17809912/parsing-iw-wlan0-scan-output

沒有留言: