Python:解析 iwlist wlan0 扫描输出并通过 Linux tty 发送

标签 python linux parsing wifi

我有一个 Python 脚本,它的作用是打印时间和 NMEA 数据。

脚本:

import serial
import struct
import time

ser = serial.Serial("/dev/ttyUSB0", 4800, timeout = 1)
date = time.time()
file = open("/home/pi/GPSWIFI.csv", "w")
file.write('\n')
for i in range(0,100):
    date = time.time()
    val = ser.readline();
    #value = val[:]
    print >> file ,date,',',val
file.close()

输出:

1407854475.74 , $GPGSA,A,3,07,30,19,08,27,,,,,,,,7.4,5.5,4.9*3D

1407854475.86 , $GPRMC,144228.109,A,5323.0774,N,00636.1034,W,0.48,332.36,120814,,,A*7E

1407854476.02 , $GPVTG,332.36,T,,M,0.48,N,0.9,K,A*0F

1407854476.11 , $GPGGA,144229.000,5323.0776,N,00636.1041,W,1,05,5.5,69.8,M,55.4,M,,0000*76

1407854476.59 , $GPGLL,5323.0776,N,00636.1041,W,144229.000,A,A*45

1407854476.71 , $GPGSA,A,3,07,30,19,08,27,,,,,,,,7.4,5.5,4.9*3D

1407854476.82 , $GPRMC,144229.000,A,5323.0776,N,00636.1041,W,0.45,331.75,120814,,,A*7E

1407854476.99 , $GPVTG,331.75,T,,M,0.45,N,0.8,K,A*07

1407854477.07 , $GPGGA,144230.000,5323.0776,N,00636.1046,W,1,05,5.5,69.7,M,55.4,M,,0000*76

我想要的是我想添加以下输出:

sudo iwlist wlan0 scan

到每行的末尾。 我希望所有 GPS 位置都有自己的无线接入点信息,例如 ESSID 和地址等。

如有任何建议,我们将不胜感激。提前致谢。

*编辑:输出 sudo iwlist wlan0 扫描 :

wlan0     Scan completed :
          Cell 01 - Address: FC:F5:28:B0:29:CB
                    Channel:4
                    Frequency:2.427 GHz (Channel 4)
                    Quality=20/100  Signal level=20/100
                    Encryption key:on
                    ESSID:"F1000a"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
                              24 Mb/s; 36 Mb/s; 54 Mb/s
                    Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
                    Mode:Master
                    Extra:tsf=0000000635284560
                    Extra: Last beacon: 0ms ago
                    IE: Unknown: 0006463130303061
                    IE: Unknown: 010882848B962430486C
                    IE: Unknown: 030104
                    IE: Unknown: 2A0100
                    IE: Unknown: 2F0100
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : CCMP TKIP
                        Authentication Suites (1) : PSK
                    IE: Unknown: 32040C121860
                    IE: Unknown: 0B0503000B0000
                    IE: Unknown: 2D1ABC1817FFFF000000000000000000000000000000000000000000
                    IE: Unknown: 3D1604001700000000000000000000000000000000000000
                    IE: Unknown: 7F03000008
                    IE: Unknown: DD860050F204104A0001101044000102103B000103104700106735A89D859A8E6F9E540FD8AD0A9123102100094D69747261537461721023000C564D47383332342D423130411024000C564D47383332342D4231304110420007393633363847571054000800060050F20400011011000C564D47383332342D42313041100800020084103C000101
                    IE: Unknown: DD090010180203000C0000
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : CCMP TKIP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
          Cell 02 - Address: 00:26:99:4D:0D:35
                    Channel:11
                    Frequency:2.462 GHz (Channel 11)
                    Quality=23/100  Signal level=23/100
                    Encryption key:on
                    ESSID:"eduroam"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=000000f26bf99c46
                    Extra: Last beacon: 0ms ago
                    IE: Unknown: 0007656475726F616D
                    IE: Unknown: 010882848B0C12961824
                    IE: Unknown: 03010B
                    IE: Unknown: 0706494520010D14
                    IE: Unknown: 0B050200278D5B
                    IE: Unknown: 2A0102
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (2) : 802.1x Proprietary
                    IE: Unknown: 32043048606C
                    IE: Unknown: 851E02008F000F00FF03590045452D472D303200000000000000000002000027
                    IE: Unknown: 9606004096000B00
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (2) : 802.1x Proprietary
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                    IE: Unknown: DD06004096010104
                    IE: Unknown: DD050040960305
                    IE: Unknown: DD050040960B09
                    IE: Unknown: DD050040961401
          Cell 03 - Address: 00:26:99:4D:0D:30
                    Channel:11
                    Frequency:2.462 GHz (Channel 11)
                    Quality=23/100  Signal level=23/100
                    Encryption key:on
                    ESSID:"CONF"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=000000f26bf88aba
                    Extra: Last beacon: 0ms ago
                    IE: Unknown: 0004434F4E46
                    IE: Unknown: 010882848B0C12961824
                    IE: Unknown: 03010B
                    IE: Unknown: 0706494520010D14
                    IE: Unknown: 0B050200278D5B
                    IE: Unknown: 2A0102
                    IE: Unknown: 32043048606C
                    IE: Unknown: 851E02008F000F00FF03590045452D472D303200000000000000000002000027
                    IE: Unknown: 9606004096000B00
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                    IE: Unknown: DD06004096010104
                    IE: Unknown: DD050040960305
                    IE: Unknown: DD050040960B09
                    IE: Unknown: DD050040961400*

最佳答案

这是我的想法:

import subprocess as s
import itertools

p = s.Popen(['/sbin/iw','wlan0','scan'], stdout=s.PIPE)
r = p.communicate()[0].replace('\n','')
# this line was missing a closing bracket and paren ------------------------vv
r = filter(None, [filter(None, item.split('\t')) for item in r.split('Cell')]) # EDITED

# an example
#r = [['WMO: asdgasdgolasikdng;laskdgn','','ERP','SSID:SGLNOE'],['BLERP'],[''],['SSID:WOWOW']]
# a_list_of_interest = ['WMO:','ERP','SSID']
# for index, cell in enumerate(r):
    # tmp_cell = []
    # for items in cell:
        # for item in a_list_of_interest:
            # if item in items:
                # tmp_cell.append(items)
        # r[index] = tmp_cell

merged = list(itertools.chain.from_iterable(r)) # merge the cells, or just use the index of one cell?
print(merged)
your_string += ','.join(merged) # add it to the line

关于Python:解析 iwlist wlan0 扫描输出并通过 Linux tty 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267587/

相关文章:

python - 通过python计算文本文件中的单词

python - QFileSystemModel()/QTreeView() : traverse model filesystem tree prior to view expansion

linux - bash 脚本 : combine var=$(. ..) 和 var=${var%%...} 行?

c - 使用指向数值数据的 void* 指针的算术运算

c++ - 带有证书验证的 native C++ HTTPS REST 调用

linux - vagrant 在 mac 的 debian VM 上设备上没有剩余空间

json - 如何将 PST 文件解析/索引到 elasticsearch?

python - Tkinter Optionmenu StringVar.get() 返回空白

python - 如何获取当前用户和其他用户之间的最后一条消息?

python - 用于在文本中查找字母和数字单词的正则表达式