linuxawkssidbssid

Obtain all WiFis' SSIDs & BSSIDs


How can I get all BSSIDs of the iw and iwinfo commands' output? Here are iw and iwinfo commands to get all the SSIDs. iw:

iw dev wlan0 scan 2>/dev/null | awk '
/SSID: / {
  if (!seen[$0]++) {
    printf "\""
    for (i = 2; i <= NF; i++) if (i == 2) printf $i
    else printf " " $i
    printf "\" "
  }
}
'

iwinfo:

iwinfo wlan0 scan | awk '
/ESSID: ".*"/ {
  ORS = " "
  if (!seen[$0]++) for (i = 2; i <= NF; i++) print $i
}
'

Current awk output:

"WiFi-1" "WiFi-2" "WiFi-3" "WiFi-4" "WiFi-5" ...

iw console output:

BSS 01:23:45:67:89:AB(on wlan0)
        TSF: 128785915910 usec (1d, 11:46:25)
        freq: 2437
        beacon interval: 200 TUs
        capability: ESS ShortPreamble ShortSlotTime (0x0421)
        signal: -71.00 dBm
        last seen: 990 ms ago
        Information elements from Probe Response frame:
        SSID: WiFi-1
        Supported rates: 1.0* 2.0* 5.5* 6.0 9.0 11.0* 12.0 18.0
        DS Parameter set: channel 6
        Country: SK     Environment: Indoor/Outdoor
                Channels [1 - 13] @ 20 dBm
        ERP: <no flags>
        Extended supported rates: 24.0 36.0 48.0 54.0
        WMM:     * Parameter version 1
                 * BE: CW 15-1023, AIFSN 3
                 * BK: CW 15-1023, AIFSN 7
                 * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
                 * VO: CW 3-7, AIFSN 2, TXOP 1504 usec
BSS CD:EF:A0:A1:A2:A3(on wlan0)
        TSF: 2381690679244 usec (27d, 13:34:50)
        freq: 2467
        beacon interval: 200 TUs
        capability: ESS ShortPreamble ShortSlotTime (0x0421)
        signal: -94.00 dBm
        last seen: 90 ms ago
        Information elements from Probe Response frame:
        SSID: WiFi-2
        Supported rates: 1.0* 2.0* 5.5* 6.0 9.0 11.0* 12.0 18.0
        DS Parameter set: channel 12
        Country: SK     Environment: Indoor/Outdoor
                Channels [1 - 13] @ 20 dBm
        ERP: <no flags>
        Extended supported rates: 24.0 36.0 48.0 54.0
        WMM:     * Parameter version 1
                 * BE: CW 15-1023, AIFSN 3
                 * BK: CW 15-1023, AIFSN 7
                 * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
                 * VO: CW 3-7, AIFSN 2, TXOP 1504 usec

iwinfo console output:

Cell 01 - Address: 01:23:45:67:89:AB
          ESSID: "WiFi-1"
          Mode: Master  Channel: 11
          Signal: -49 dBm  Quality: 61/70
          Encryption: WPA2 PSK (CCMP)

Cell 02 - Address: CD:EF:A0:A1:A2:A3
          ESSID: "WiFi-2"
          Mode: Master  Channel: 11
          Signal: -53 dBm  Quality: 57/70
          Encryption: WPA2 PSK (CCMP)

I would like to get the following output using the awk:

"01:23:45:67:89:AB" "CD:EF:A0:A1:A2:A3" ...

What is the correct way to capture all the BSSIDs using the Awk for both libs (iw & iwinfo)?


Solution

  • With GNU awk for the 3rd arg to match():

    { cat iw_output; cat iwinfo_output; } |
    awk 'match($0,/([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}/,a) && !seen[a[0]]++{print a[0]}'
    01:23:45:67:89:AB
    CD:EF:A0:A1:A2:A3
    

    or to get the output format requested in your question:

    { cat iw_output; cat iwinfo_output; } |
    awk 'match($0,/([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}/,a) && !seen[a[0]]++{printf "%s\"%s\"", (c++?OFS:""), a[0]} END{print ""}'
    "01:23:45:67:89:AB" "CD:EF:A0:A1:A2:A3"
    

    With other awks you'd use substr($0,RSTART,RLENGTH) instead of a[0].