我有这个python脚本:
import subprocess
import csv
def wifi_sniffer(ssid=''):
""" Perform wifi scan and calls function to export results into a CSV file. """
cmd = 'airport -s | grep -i ' + str(ssid) if ssid <> '' else 'airport -s'
wifi = str(subprocess.check_output(cmd, shell=True)).split('\n')
wifi_arr = []
for item in wifi:
wifi_arr.append(item.strip())
wifi_arr.pop(0)
export_to_csv(wifi_arr)
def export_to_csv(array):
""" Exports wifi scan result into a CSV file. """
array.pop(len(array)-1)
f = open('export.csv', 'wt')
try:
writer = csv.writer(f)
for i in array:
#print i
writer.writerow((str(i.split(' ')[0]), str(i.split(' ')[2])))
finally:
f.close()
""" Call main function. """
wifi_sniffer()
这就是我的结果:
Vix_CPD c8:d7:19:fb:97:ee -93 149,+1 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
Prixvisitante 0a:27:22:f3:3d:e5 -67 1 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
Superprix 06:27:22:f3:3d:e5 -70 1 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
Vivo 4G Plus 08:63:61:8d:95:dc -85 11 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
@n18u$Ch@c@l 58:8d:09:1d:c5:a1 -81 11 N US WPA2(802.1x/AES/AES)
ASM Wifi 00:1a:3f:4b:b4:b2 -79 11 N BR WPA(PSK/TKIP,AES/TKIP) WPA2(PSK/TKIP,AES/TKIP)
Visitantes 58:8d:09:1d:c5:a0 -79 11 N US NONE
LEMON 74:ea:3a:fc:a9:68 -81 10,-1 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
Superprix 06:27:22:f3:80:9b -33 6 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
Prixvisitante 0a:27:22:f3:80:9b -34 6 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
我需要拆分字符串以获取SSID和信号强度,但是当我按空格分割时,像Vivo 4G Plus
这样的SSID会将Vivo
作为索引0返回,4G
作为索引2返回
我需要将Vivo 4G Plus
作为索引0并将-85
作为索引2。
答案 0 :(得分:4)
正则表达式可以提供帮助:
r'(?P<ssid>.*?)\s+(?P<mac>(?:[a-zA-Z\d]{2}:){5}[a-zA-Z\d]{2})\s+(?P<signal>[-\d]+)'
这将把SSID,MAC地址和信号强度提取为单独的字段。
关键部分是(?P<mac>...)
组;它只匹配由冒号分隔的6个2位十六进制值;然后,上一节将抓取所有,直到MAC地址前的空白,(?P<signal>...)
组接下来的数字和减号之后的任何内容。
交互式会话中的演示:
>>> import re
>>> sample = '''\
... Vix_CPD c8:d7:19:fb:97:ee -93 149,+1 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
... Prixvisitante 0a:27:22:f3:3d:e5 -67 1 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
... Superprix 06:27:22:f3:3d:e5 -70 1 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
... Vivo 4G Plus 08:63:61:8d:95:dc -85 11 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
... @n18u$Ch@c@l 58:8d:09:1d:c5:a1 -81 11 N US WPA2(802.1x/AES/AES)
... ASM Wifi 00:1a:3f:4b:b4:b2 -79 11 N BR WPA(PSK/TKIP,AES/TKIP) WPA2(PSK/TKIP,AES/TKIP)
... Visitantes 58:8d:09:1d:c5:a0 -79 11 N US NONE
... LEMON 74:ea:3a:fc:a9:68 -81 10,-1 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
... Superprix 06:27:22:f3:80:9b -33 6 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
... Prixvisitante 0a:27:22:f3:80:9b -34 6 Y -- WPA(PSK/AES/AES) WPA2(PSK/AES/AES)
... '''.splitlines()
>>> pattern = re.compile(r'(?P<ssid>.*?)\s+(?P<mac>(?:[a-zA-Z\d]{2}:){5}[a-zA-Z\d]{2})\s+(?P<signal>[-\d]+)')
>>> for line in sample:
... print(pattern.match(line).groupdict())
...
{'mac': 'c8:d7:19:fb:97:ee', 'ssid': 'Vix_CPD', 'signal': '-93'}
{'mac': '0a:27:22:f3:3d:e5', 'ssid': 'Prixvisitante', 'signal': '-67'}
{'mac': '06:27:22:f3:3d:e5', 'ssid': 'Superprix', 'signal': '-70'}
{'mac': '08:63:61:8d:95:dc', 'ssid': 'Vivo 4G Plus', 'signal': '-85'}
{'mac': '58:8d:09:1d:c5:a1', 'ssid': '@n18u$Ch@c@l', 'signal': '-81'}
{'mac': '00:1a:3f:4b:b4:b2', 'ssid': 'ASM Wifi', 'signal': '-79'}
{'mac': '58:8d:09:1d:c5:a0', 'ssid': 'Visitantes', 'signal': '-79'}
{'mac': '74:ea:3a:fc:a9:68', 'ssid': 'LEMON', 'signal': '-81'}
{'mac': '06:27:22:f3:80:9b', 'ssid': 'Superprix', 'signal': '-33'}
{'mac': '0a:27:22:f3:80:9b', 'ssid': 'Prixvisitante', 'signal': '-34'}