2010-09-13 113 views
2

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

我希望这里可以问这里。

回答

1

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

1

由于/proc/net/wireless只是显示有关当前WLAN连接的信息,我修改了一个脚本以包含必要的加密信息。喂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然后它们由"|"分隔的多个加密机制。