获取所有WiFi的SSID和BSSID

Obtain all WiFis' SSIDs & BSSIDs

如何获取 iw 和 iwinfo 命令输出的所有 BSSID?这是获取所有 SSID 的 iw 和 iwinfo 命令。我:

iw dev wlan0 scan 2>/dev/null | awk '
/SSID: / {
  if (!seen[[=10=]]++) {
    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[[=11=]]++) for (i = 2; i <= NF; i++) print $i
}
'

当前 awk 输出:

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

iw 控制台输出:

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 控制台输出:

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)

我想使用 awk 获得以下输出:

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

使用 Awk 为两个库(iw 和 iwinfo)捕获所有 BSSID 的正确方法是什么?

更新答案

如果你想让输出唯一,你会 运行 通过 sortuniq:

我的原始答案中的命令结果
{ iw ...; iwinfo ...; } | grep -Eo '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sort | uniq

如果你想将它们用双引号括起来并用 space 替换换行符,你可以这样做:

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

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

原答案

我对你的问题感到困惑,但我认为这会满足你的要求:

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

示例输出

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

@Martin:尝试:

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

使用 GNU awk 匹配第三个参数():

{ cat iw_output; cat iwinfo_output; } |
awk 'match([=10=],/([[: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

或获取您问题中要求的输出格式:

{ cat iw_output; cat iwinfo_output; } |
awk 'match([=11=],/([[: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"

对于其他 awk,您将使用 substr([=12=],RSTART,RLENGTH) 而不是 a[0]