简体   繁体   English

获取所有WiFi的SSID和BSSID

[英]Obtain all WiFis' SSIDs & BSSIDs

How can I get all BSSIDs of the iw and iwinfo commands' output? 如何获取iw和iwinfo命令输出的所有BSSID? Here are iw and iwinfo commands to get all the SSIDs. 这是iw和iwinfo命令,用于获取所有SSID。 iw: 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:

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

Current awk output: 当前的awk输出:

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

iw console output: 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 console output: 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)

I would like to get the following output using the awk: 我想使用awk获得以下输出:

"01:23:45:67:89:AB" "CD:EF:A0:A1:A2:A3" ... “ 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)? 使用两个库的Awk(iw和iwinfo)捕获所有BSSID的正确方法是什么?

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 : 如果要使输出唯一,则可以通过sortuniq在我的原始答案中运行命令的结果:

{ 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

@马丁:尝试:

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

With GNU awk for the 3rd arg to match(): 使用GNU awk作为第三个参数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] . 对于其他awk,您将使用substr($0,RSTART,RLENGTH)而不是a[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM