iwlist扫描输出格式

时间:2010-09-13 18:35:33

标签: python linux parsing wifi

我必须编写一个工具来从iwlist扫描中获取加密类型。我似乎无法找到是否有标准输出。谷歌搜索它似乎人们发布略有不同的格式,但我不知道他们是否只是复制/粘贴错误或什么。具体来说,在Encryption key: On中,On / Off首字母是否总是大写? IE: IEEE 802.11i/WPA2 Version 1怎么样?加密是否始终以IEEE 802.11i/

开头

我希望可以在这里问一下。

2 个答案:

答案 0 :(得分:1)

根据您的需要,您可能最好解析/proc/net/wireless的内容。 This将帮助您入门。这些字段都是相同的,但值可能因驱动程序和驱动程序以及设备而异。所以不,你可能不能依靠拼写来保持一致,而且资本化更是如此。

答案 1 :(得分:1)

由于/proc/net/wireless仅显示有关当前WLAN连接的信息,因此我调整了一个脚本以包含例如必要的加密信息。 Feed wpa_supplicant

#!/bin/bash
while read line; do

    ## Reset variables on new network
    [[ "$line" =~ Cell || "$line" == "" ]] && {

        # If no WPA encryption info was found though "Encryption" was "On", then we have WEP
        [[ "$encryption" == "" && "$enc" =~ On ]] && encryption = "WEP"

        # If we already found one network then echo its information
        [[ "$network" != "" ]] && echo "$network [$encryption]"
        network=""
        encryption=""
    }

    ## Test line content and parse as required
    [[ "$line" =~ Address ]] && mac=${line##*ss: }
    [[ "$line" =~ \(Channel ]] && { chn=${line##*nel }; chn=${chn:0:$((${#chn}-1))}; }
    [[ "$line" =~ Frequen ]] && { frq=${line##*ncy:}; frq=${frq%% *}; }
    [[ "$line" =~ Quality ]] && {
        qual=${line##*ity=}
        qual=${qual%% *}
        lvl=${line##*evel=}
        lvl=${lvl%% *}
    }

    ## Encryption is "On" if WEP or WPA, otherwise it's "Open"
    [[ "$line" =~ Encrypt ]] && enc=${line##*key:}
    [[ "$enc" =~ Off ]] && {
        [[ "$encryption" != "" ]] && encryption="${encryption},"
        encryption="${encryption}Open"
    }

    ## The ESSID is the last line of the basic channel data, so build information string now
    [[ "$line" =~ ESSID ]] && {
        essid=${line##*ID:}
        network="$mac  $essid  $frq  $chn  $qual  $lvl  $enc"  # output after ESSID
    }

    ## WPA encryption information
    [[ "$line" =~ WPA ]] && wpa=${line##*WPA} && {
        [[ "$encryption" != "" ]] && encryption="${encryption}|"
        encryption="${encryption}WPA$wpa"
    }
    [[ "$line" =~ "Group Cipher" ]] && encryption="$encryption,${line##*: }"
    [[ "$line" =~ "Pairwise Cipher" ]] && encryption="$encryption,${line##*: }"
    [[ "$line" =~ "Authentication Suites" ]] && encryption="$encryption,${line##*: }"

done < <(iwlist wlan0 scan 2>/dev/null )

脚本输出(示例):

34:81:C7:EB:24:89  "cyberdyne"  2.462  11  67/70  -43  on [WPA2 Version 1,CCMP,CCMP,PSK]
36:81:C7:EB:24:89  "cyberguest"  2.462  11  65/70  -45  on [WPA2 Version 1,TKIP,CCMP,PSK|WPA Version 1,TKIP,TKIP,PSK]

如果SSID有多种可用的加密机制,则它们由"|"隔开。