Obtain all WiFis' SSIDs & BSSIDs

3k Views Asked by At

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

4

There are 4 best solutions below

2
On BEST ANSWER

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].

2
On

Updated Answer

If you want to make the outputs unique, you would run the result of the commands in my original answer through sort and uniq:

{ iw ...; iwinfo ...; } | grep -Eo '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sort | uniq

If you then wanted to wrap them in double quotes and replace the newline with a space, you would do:

{ iw ...; iwinfo ...; } | grep -Eo '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sort | uniq | sed -E 's|(.*)|"\1"|' | tr '\n' ' '

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

Original Answer

I am confused by your question, but I think this will do what you want:

{ iw dev wlan0 scan 2>/dev/null; iwinfo wlan0 scan; } | grep -Eo '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Sample Output

01:23:45:67:89:AB
CD:EF:A0:A1:A2:A3
01:23:45:67:89:AB
CD:EF:A0:A1:A2:A3
0
On

@Martin: Try:

Your_command | awk -vs1="\"" '/^Cell/{VAL=VAL?VAL s1 $NF s1:s1 $NF s1} END{print VAL}' 
1
On

I've tried pretty much every solution here - none of them worked for me, it seems the best idea is to run:

wsl --unregister kali-linux

and then install it again via the microsoft store.