从shell输出中提取特定字符

时间:2017-05-29 07:03:39

标签: bash shell awk sed

执行时:

root@imx6slzbha:~# iw wlan0 scan

我收到以下输出信息,它为我提供了多个附近的SSID数据。

如何根据特定的SSID名称获取信号强度?

目前,我正在使用此命令基于BSS获取:

iw wlan0 scan | sed -n '/bc:d1:1f:16:55:c7/,/WMM/p' | grep signal | sed 's/.*-//' | awk '{print $1}' | cut -d . -f 1

但我可能每次都不知道BSS,因此需要对此要求作出通用答案。enter image description here

BSS c8:00:84:85:41:b1 (on wlan0)
        TSF: 4138861160471 usec (47d, 21:41:01)
        freq: 2462
        beacon interval: 102
        capability: ESS ShortPreamble ShortSlotTime (0x1421)
        signal: -80.00 dBm
        last seen: 90 ms ago
        SSID: Swarovski_Guest
        Supported rates: 1.0* 2.0* 5.5* 6.0 9.0 11.0* 12.0 18.0
        DS Parameter set: channel 11
        Country: IN     Environment: Indoor/Outdoor
                Channels [1 - 11] @ 30 dBm
        ERP: <no flags>
        HT capabilities:
                Capabilities: 0x19ac
                        HT20
                        SM Power Save disabled
                        RX HT20 SGI
                        TX STBC
                        RX STBC 1-stream
                        Max AMSDU length: 7935 bytes
                        DSSS/CCK HT40
                Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
                Minimum RX AMPDU time spacing: 8 usec (0x06)
                HT RX MCS rate indexes supported: 0-23
                HT TX MCS rate indexes are undefined
        Extended supported rates: 24.0 36.0 48.0 54.0
        HT operation:
                 * primary channel: 11
                 * secondary channel offset: no secondary
                 * STA channel width: 20 MHz
                 * RIFS: 0
                 * HT protection: nonmember
                 * non-GF present: 1
                 * OBSS non-GF present: 0
                 * dual beacon: 0
                 * dual CTS protection: 0
                 * STBC beacon: 0
                 * L-SIG TXOP Prot: 0
                 * PCO active: 0
                 * PCO phase: 0
        Extended capabilities: 4, 6
        WMM:     * Parameter version 1
                 * u-APSD
                 * BE: CW 15-1023, AIFSN 3
                 * BK: CW 15-1023, AIFSN 7
                 * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
                 * VO: acm CW 3-7, AIFSN 2, TXOP 1504 usec
BSS a0:ab:1b:cf:28:ae (on wlan0)
        TSF: 3196211626 usec (0d, 00:53:16)
        freq: 2462
        beacon interval: 100
        capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)
        signal: -74.00 dBm
        last seen: 80 ms ago
        Information elements from Probe Response frame:
        SSID: FOTA_Rashmi_2.4G
        Supported rates: 1.0* 2.0* 5.5* 11.0* 9.0 18.0 36.0 54.0
        DS Parameter set: channel 11
        Extended supported rates: 6.0 12.0 24.0 48.0
        Country: EU     Environment: Indoor/Outdoor
                Channels [1 - 13] @ 20 dBm
        TIM: DTIM Count 0 DTIM Period 1 Bitmap Control 0x0 Bitmap[0] 0x2
        WPS:     * Version: 1.0
                 * Wi-Fi Protected Setup State: 2 (Configured)
                 * UUID: 28802880-2880-1880-a880-a0ab1bcf28ae
                 * RF Bands: 0x1
                 * Unknown TLV (0x1049, 6 bytes): 00 37 2a 00 01 20
        ERP: <no flags>
        HT capabilities:
                Capabilities: 0x11ee
                        HT20/HT40
                        SM Power Save disabled
                        RX HT20 SGI
                        RX HT40 SGI
                        TX STBC
                        RX STBC 1-stream
                        Max AMSDU length: 3839 bytes
                        DSSS/CCK HT40
                Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
                Minimum RX AMPDU time spacing: 4 usec (0x05)
                HT RX MCS rate indexes supported: 0-15, 32
                HT TX MCS rate indexes are undefined
        HT operation:
                 * primary channel: 11
                 * secondary channel offset: no secondary
                 * STA channel width: 20 MHz
                 * RIFS: 0
                 * HT protection: nonmember
                 * non-GF present: 0
                 * OBSS non-GF present: 0
                 * dual beacon: 0
                 * dual CTS protection: 0
                 * STBC beacon: 0
                 * L-SIG TXOP Prot: 0
                 * PCO active: 0
                 * PCO phase: 0
        Extended capabilities: HT Information Exchange Supported
        WPA:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
        RSN:     * Version: 1
                 * Group cipher: TKIP
                 * Pairwise ciphers: TKIP CCMP
                 * Authentication suites: PSK
                 * Capabilities: (0x0000)
        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

1 个答案:

答案 0 :(得分:1)

为了使用给定的SSID,例如“Swarovski_Guest”你可以:

sed -n '/signal/h;/SSID: Swarovski_Guest/{x;s/^.*: -\(.*\) dBm$/\1/;p}'
  • 寻找信号强度/signal/
  • 将其存储在历史记录h;
  • 查找特定的SSID名称/SSID: Swarovski_Guest/
  • 检索信号强度x;
  • 修剪除值s/^.*: -\(.*\) dBm$/\1/;
  • 之外的任何内容
  • 打印p

样本输入的输出:

 80.00