从输出中仅获取 ssid = subprocess.check_output(list_networks_command, shell=True, text=False)

问题描述 投票:0回答:1

我用 python 编写了一段代码,为我提供了所有可用的网络:

# importing the subprocess module

import subprocess

list_networks_command = 'nmcli -f SSID dev wifi list'

# Execute the command and capture the result.

output = subprocess.check_output(list_networks_command, shell=True, text=True)
output = output.replace(" ", "-")

# Print the output, all networks in range.

print(output)

输出: enter image description here

问题是我只想打印例如网络:“OnePluse 5T”。 如果网络名称后面有空格,并且我不想要网络名称后面的空格,我只想要网络名称,我该如何执行此操作。 请帮助我

我尝试列出一个列表并找到第一项,但它也给了我网络名称后面的空格

list subprocess
1个回答
0
投票

您可以尝试使用

subprocess.check_output(['iw', 'dev', 'wlan0', 'scan', '|', 'grep', 'SSID'])
而不是
subprocess.check_output(list_networks_command, shell=True, text=True)

© www.soinside.com 2019 - 2024. All rights reserved.