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

时间:2014-08-12 14:55:54

标签: python linux parsing wlan

我有一个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 scan :

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*

1 个答案:

答案 0 :(得分:0)

以下是我提出的建议:

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