gpt4 book ai didi

python - 无法按空格正确拆分 Wi-Fi 信号字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:23 25 4
gpt4 key购买 nike

我有这个 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。

最佳答案

正则表达式可以帮助:

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>...) group 接受后面带数字和减号的任何内容。

Online demo at regex101 .

在交互式 session 中演示:

>>> 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'}

关于python - 无法按空格正确拆分 Wi-Fi 信号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25692323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com