iwlist输出到列表或变量

时间:2017-04-21 17:26:13

标签: linux awk

我试图创建一个php网页,让用户连接到无线网络。自从"无线工具"是唯一可用的选项,我使用iwlist通过以下命令显示可用的wifi网络:

iwlist wlan0 scan | egrep 'Cell |Quality|ESSID|Auth|key|IE: IEEE|IE: WPA' 

这给了我以下输出:

     Cell 01 - Address: 88:03:55:E8:3A:D0
                Quality=23/70  Signal level=-87 dBm  
                Encryption key:on
                ESSID:"VGV7519E83ADB"
                IE: WPA Version 1
                    Authentication Suites (1) : PSK
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK
      Cell 02 - Address: 88:03:55:E8:3A:D1
                Quality=23/70  Signal level=-87 dBm  
                Encryption key:off
                ESSID:"KPN Fon"
      Cell 03 - Address: 90:5C:44:C5:B8:9D
                Quality=59/70  Signal level=-51 dBm  
                Encryption key:on
                ESSID:"Doggie"
                IE: WPA Version 1
                    Authentication Suites (1) : PSK
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK
      Cell 04 - Address: 54:FA:3E:60:F9:B1
                Quality=22/70  Signal level=-88 dBm  
                Encryption key:on
                ESSID:"HZN249093067"
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK
      Cell 05 - Address: DC:71:44:FA:1D:17
                Quality=27/70  Signal level=-83 dBm  
                Encryption key:on
                ESSID:"UPC244948707"
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK
      Cell 06 - Address: 64:D1:A3:31:D1:6C
                Quality=24/70  Signal level=-86 dBm  
                Encryption key:on
                ESSID:"Sitecom31D16A"
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK
      Cell 07 - Address: F8:04:2E:30:84:31
                Quality=22/70  Signal level=-88 dBm  
                Encryption key:on
                ESSID:"UPC242176060"
                IE: IEEE 802.11i/WPA2 Version 1
                    Authentication Suites (1) : PSK

因为我想列出使用哪种类型的身份验证我会显示加密,IE:IEEE和IE:WPA列出WEP,WPA1和WPA2 PSK

有没有办法将结果保存在这样的变量中?

Cell 01 (88:03:55:E8:3A:D0, VGV7519E83ADB, WPA, WPA2 PSK, 23/70, -87)
Cell 02 (88:03:55:E8:3A:D1, KPN Fon, WEP, , 23/70, -87)
Cell 03 (90:5C:44:C5:B8:9D, Doggie, WPA, WPA2 PSK,  59/70, -51)
etc...

我尝试过使用AWK(gawk不可用)但没有成功..

的结果
iwlist wlan0 scan

      wlan0     Scan completed :
      Cell 01 - Address: 88:03:55:E8:3A:D1
                Channel:1
                Frequency:2.412 GHz (Channel 1)
                Quality=29/70  Signal level=-81 dBm  
                Encryption key:off
                ESSID:"KPN Fon"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                          18 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=000009e127b7513e
                Extra: Last beacon: 6290ms ago
                IE: Unknown: 00074B504E20466F6E
                IE: Unknown: 010882848B961224486C
                IE: Unknown: 030101
                IE: Unknown: 2A0104
                IE: Unknown: 32040C183060
                IE: Unknown: 2D1A6C0017FFFF0000000000000000000000000000000C0000000000
                IE: Unknown: 3D1601000400000000000000000000000000000000000000
                IE: Unknown: 3E0100
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
                IE: Unknown: 0B0504001C127A
                IE: Unknown: 7F0101
                IE: Unknown: DD8F0050F204104A00011010440001021041000100103B0001031047001000000000000000030000880355E83ADB1021000B436F72706F726174696F6E1023000B564756373531394B5732321024000930322E30302E3133361042000A413334343030333035311054000800060050F204000110110014576972656C65737320526F757465722857464129100800020084
                IE: Unknown: 07064E4C20010D10
      Cell 02 - Address: 90:5C:44:C5:B8:9D
                Channel:11
                Frequency:2.462 GHz (Channel 11)
                Quality=58/70  Signal level=-52 dBm  
                Encryption key:on
                ESSID:"Doggie"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                          18 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=000001c683317915
                Extra: Last beacon: 90ms ago
                IE: Unknown: 00124368696E6565732052657374617572616E74
                IE: Unknown: 010882848B961224486C
                IE: Unknown: 03010B
                IE: Unknown: 2A0104
                IE: Unknown: 32040C183060
                IE: Unknown: 0706455520010D14
                IE: Unknown: 2D1AAC0117FFFF000000000000000000000000000000000000000000
                IE: Unknown: 3D160B000700000000000000000000000000000000000000
                IE: WPA Version 1
                    Group Cipher : TKIP
                    Pairwise Ciphers (2) : TKIP CCMP
                    Authentication Suites (1) : PSK
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : TKIP
                    Pairwise Ciphers (2) : TKIP CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: 7F080100000000000000
                IE: Unknown: 0B05040031127A
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
                IE: Unknown: 7F080100000000000000
                IE: Unknown: 0706455520010D10
                IE: Unknown: DDA70050F204104A0001101044000102103B00010310470010E6825C801DD411B2860188B76881A5BF1021001852616C696E6B20546563686E6F6C6F67792C20436F72702E1023001C52616C696E6B20576972656C6573732041636365737320506F696E74102400065254323836301042000831323334353637381054000800060050F20400011011000952616C696E6B415053100800020000103C0001011049000600372A000120
                IE: Unknown: DD07000C4300000000
      Cell 03 - Address: 54:FA:3E:60:F9:B1
                Channel:11
                Frequency:2.462 GHz (Channel 11)
                Quality=21/70  Signal level=-89 dBm  
                Encryption key:on
                ESSID:"HZN249093067"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                          18 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=00000144750d9416
                Extra: Last beacon: 170ms ago
                IE: Unknown: 000C485A4E323439303933303637
                IE: Unknown: 010882848B961224486C
                IE: Unknown: 03010B
                IE: Unknown: 2A0104
                IE: Unknown: 32040C183060
                IE: Unknown: 2D1AEC0103FFFF0000000000000000000000000000000C0000000000
                IE: Unknown: 3D160B000100000000000000000000000000000000000000
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : TKIP
                    Pairwise Ciphers (2) : TKIP CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                IE: Unknown: 0B05000027127A
                IE: Unknown: 7F0101
                IE: Unknown: DD07000C4307000000
                IE: Unknown: 07064E4C20010D10
                IE: Unknown: DDA70050F204104A0001101044000102103B00010310470010BC329E001DD811B2860154FA3E60F9B71021001A43656C656E6F20436F6D6D756E69636174696F6E2C20496E632E1023001743656C656E6F20576972656C65737320415020322E344710240006434C313830301042000831323334353637381054000800060050F20400011011000C43656C656E6F4150322E3447100800024388103C0001011049000600372A000120
      Cell 04 - Address: 64:D1:A3:31:D1:6C
                Channel:8
                Frequency:2.447 GHz (Channel 8)
                Quality=28/70  Signal level=-82 dBm  
                Encryption key:on
                ESSID:"Sitecom31D16A"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                          18 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=0000000bc7aa0889
                Extra: Last beacon: 20750ms ago
                IE: Unknown: 000D53697465636F6D333144313641
                IE: Unknown: 010882848B961224486C
                IE: Unknown: 030108
                IE: Unknown: 2A0102
                IE: Unknown: 32040C183060
                IE: Unknown: 2D1AEE1117FFFF0000010000000000000000000000000C0000000000
                IE: Unknown: 3D1608050500000000000000000000000000000000000000
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                IE: Unknown: 0B0502000C127A
                IE: Unknown: DD07000C4307000000
                IE: Unknown: DDDE0050F204104A00011010440001021057000101103B0001031047001027A9A0BC149D4DD5AB6864D1A331D16A1021001153697465636F6D204575726F70652042561023001F53697465636F6D20576972656C657373204769676162697420526F757465721024001F53697465636F6D20576972656C657373204769676162697420526F757465721042000F574C523430303434333831363239321054000800060050F20400011011001F53697465636F6D20576972656C657373204769676162697420526F7574657210080002228C103C0001011049000600372A000120
      Cell 05 - Address: 88:03:55:E8:3A:D0
                Channel:1
                Frequency:2.412 GHz (Channel 1)
                Quality=30/70  Signal level=-80 dBm  
                Encryption key:on
                ESSID:"VGV7519E83ADB"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                          18 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=000009e127b72128
                Extra: Last beacon: 1680ms ago
                IE: Unknown: 000D56475637353139453833414442
                IE: Unknown: 010882848B961224486C
                IE: Unknown: 030101
                IE: Unknown: 2A0104
                IE: Unknown: 32040C183060
                IE: Unknown: 2D1A6C0017FFFF0000000000000000000000000000000C0000000000
                IE: Unknown: 3D1601000400000000000000000000000000000000000000
                IE: Unknown: 3E0100
                IE: WPA Version 1
                    Group Cipher : TKIP
                    Pairwise Ciphers (1) : TKIP
                    Authentication Suites (1) : PSK
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : TKIP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
                IE: Unknown: 0B0504001C127A
                IE: Unknown: 7F0101
                IE: Unknown: DD8F0050F204104A00011010440001021041000100103B0001031047001000000000000000030000880355E83ADB1021000B436F72706F726174696F6E1023000B564756373531394B5732321024000930322E30302E3133361042000A413334343030333035311054000800060050F204000110110014576972656C65737320526F757465722857464129100800020084
                IE: Unknown: 07064E4C20010D10

2 个答案:

答案 0 :(得分:1)

iwlist wlan0 scan切换到iw wlan0 scan,这样可以提供更清晰的输出。 当我偶然发现Djey1301的GitHub评论时,几乎得到了我的目标:29 sept 2016

iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f scan.awk

scan.awk:

BEGIN {

    printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n","MAC","SSID","freq","signal","sig%","WPA","WPA2","WEP","TKIP","CCMP");

}
$1 == "SSID:" { buf = $2; for (i = 3; i <= NF; ++i) { buf = buf" "$i } ; e["SSID"] = buf ; next }
NF > 0{
if ($1 == "BSS") {
if( $2 ~ /^[a-z0-9:]{17}$/ ) {
if( e["MAC"] ){
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",e["MAC"],e["SSID"],e["freq"],e["sig"],e["sig%"],e["WPA"],e["WPA2"],e["WEP"],e["TKIP"],e["CCMP"]);
}
    e["MAC"] = $2;
    e["WPA"] = "n";
    e["WPA2"] = "n";
    e["WEP"] = "n";
    e["TKIP"] = "n";
    e["CCMP"] = "n";
    }
}
if ($1 == "SSID:") {
e["SSID"] = $2;
}
if ($1 == "freq:") {
     e["freq"] = $NF;
}
if ($1 == "signal:") {
     e["sig"] = $2 " " $3;
     e["sig%"] = (60 - ((-$2) - 40)) * 100 / 60;
}
if ($1 == "WPA:") {
     e["WPA"] = "y";
}
if ($1 == "RSN:") {
     e["WPA2"] = "y";
}
if ($1 == "WEP:") {
     e["WEP"] = "y";
}
if ($4 == "CCMP" || $5 == "CCMP") {
     e["CCMP"] = "y";
}
if ($4 == "TKIP" || $5 == "TKIP") {
     e["TKIP"] = "y";
}
}
END {
      printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",e["MAC"],e["SSID"],e["freq"],e["sig"],e["sig%"],e["WPA"],e["WPA2"],e["WEP"],e["TKIP"],e["CCMP"]);
}

结果我很满意:

MAC|SSID|freq|signal|sig%|WPA|WPA2|WEP|TKIP|CCMP
9B:5c:44:c5:b8:92|Chinees|2437|-78.00 dBm|36.6667|y|y|n|y|y
89:03:55:e8:3a:d2|VGV7519E83ADB|2412|-83.00 dBm|28.3333|y|y|n|y|y
89:03:55:e8:3a:2b|KPN|2412|-88.00 dBm|20|n|n|n|n|n
62:d1:a3:31:d1:61|Sitecom31D16A|2452|-89.00 dBm|18.3333|n|y|n|n|y

Scheff

的帮助下更新

答案 1 :(得分:0)

根据你的解释,我在awk开始了一个简单的解析器。实际上,我想知道为什么你排除gawk。 (您也标记了linux。您是否真的拥有 K&amp; R awk 而不是gawk的Linux发行版?)

然而,gawk会提供更多便利,但也应该可以。因为我在我的cygwin中实际上只有gawk,所以我将其与-c一起使用,这应该将其限制为“传统的awk”。

不幸的是,我完全不了解输出格式的逻辑,即如何制作这些, WEP,, WPA, WPA2 PSK件。顺便说一句。我相信您的第二个样本列表中的某些设置已更改。因此,我把这部分遗漏了,并向你展示其余部分。按照这个概念,你应该很容易自己添加遗失的休息。

档案list-wlan.awk

BEGIN {
  #debug = 1
  # states
  cell = "" # current cell
  addr = "" # current address
  name = "" # current name
  qual = "" # current quality
  slvl = "" # current signal level
}

END {
  # flush last cell
  flushCell()
}

/^[ ]*Cell [0-9]+/ { # start of new cell
  # flush previous cell
  flushCell()
  # skip prefix spaces
  sub(/[ ]+/, "") # ; print $0
  # extract cell and address from $0
  sep = index($0, " - Address: ")
  cell = substr($0, 1, sep)
  addr = substr($0, sep + 12)
  # debug
  if (debug) print "cell: "cell" addr: "addr
}

/^[ ]*Quality=/ { # quality/signal level
  # skip prefix spaces
  sub(/[ ]+/, "") # ; print $0
  # extract quality
  match($0, /[0-9/]+/) ; qual = substr($0, RSTART, RLENGTH)
  $0 = substr($0, RSTART + RLENGTH)
  # extract signal level
  match($0, /[-0-9]+/) ; slvl = substr($0, RSTART, RLENGTH)
  # debug
  if (debug) print "qual: "qual" slvl: "slvl
}

/^[ ]*ESSID:/ { # name
  # skip prefix spaces
  sub(/[ ]+/, "") # ; print $0
  # extract name
  match($0, /"[^"]*"/) ; name = substr($0, RSTART + 1, RLENGTH - 2)
  # debug
  if (debug) print "name: '"name"'"
}

function flushCell()
{
  if (cell == "") return # nothing to flush
  printf "%s(%s, %s", cell, addr, name
  ## @todo missing authentification params...
  print ", "qual", "slvl")"
}

我已将您的第二个示例输入保存到文件wlan.txt中以进行调试和演示。我在Windows 10(64位)上用bash / cygwin做了这个:

$ awk -c -f list-wlan.awk wlan.txt 
Cell 01 (88:03:55:E8:3A:D1, KPN Fon, 29/70, -81)
Cell 02 (90:5C:44:C5:B8:9D, Doggie, 58/70, -52)
Cell 03 (54:FA:3E:60:F9:B1, HZN249093067, 21/70, -89)
Cell 04 (64:D1:A3:31:D1:6C, Sitecom31D16A, 28/70, -82)
Cell 05 (88:03:55:E8:3A:D0, VGV7519E83ADB, 30/70, -80)

$

注意:

iwlist似乎提供了引用的网络名称(抱歉,ESSID)。所以,这让我对逃避感到怀疑:是否有可能逃脱?什么逃避? (当网络以非字母数字字符(尤其是引号)命名时,这将是多么令人感兴趣。)这必须进行整理。结果要么必须应用复杂的unescaping,要么应该简单地使用所有内容(包括引号)...... 目前,简单地删除了网络名称的引用。