从wpa_cli状态获取p2p_device_address

时间:2015-01-13 12:27:17

标签: python linux wpa

有没有办法从命令" 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返回会更加扼要只有地址,如果有的话?

1 个答案:

答案 0 :(得分:0)

尝试在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唯一的一行。