有什么方法可以从命令“ wpa_cli status”中获取不同的信息,例如,
p2p_device_address,ssid等,分别?

我当前正在使用的是从python脚本调用wpa_cli:

import subprocess
cmd =  "wpa_cli -i wlan3 status"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)
output = p.stdout.read()


我的输出显示为:

bssid=xx:xx:xx:xx:xx:xx
ssid=lifa_2
id=0
mode=station
pairwise_cipher=CCMP
group_cipher=CCMP
key_mgmt=WPA2-PSK
wpa_state=COMPLETED
ip_address=192.168.0.104
p2p_device_address=xx:xx:xx:xx:xx:xx
address=xx:xx:xx:xx:xx:xx
uuid=xxxxx-xxxx-xxxx-xxxx-xxxxxxx


我在这里想要的只是p2p_device_address,即

output = p.stdout.read() # in this case ouput must be a string containing only the address


我在想自己的事情是使用str.split()并选择第9行,并进行一些字符串操作以获取ID,但是这将是一个hack,让wpa_cli仅返回地址会更加麻烦,如果有的话?

最佳答案

尝试在Linux中使用pipe

import subprocess
cmd =  "wpa_cli -i wlan3 status | grep ip_address"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)
output = p.stdout.read()


然后,您将只获得ip_address行。

关于python - 从wpa_cli状态获取p2p_device_address,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27922190/

10-12 00:18
查看更多