使用“执行linux命令”|&引用;及'\n'&引用;用Python
我尝试用Python执行linux命令,但没有得到任何输出和错误使用“执行linux命令”|&引用;及'\n'&引用;用Python,python,linux,Python,Linux,我尝试用Python执行linux命令,但没有得到任何输出和错误 import subprocess, os cmd = ["iwconfig", "wlan0", "|", "grep", "ESSID", "|", "awk", "-F:", "'{print $2}'", "|", "sed", "'s/\"//g'"] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output,
import subprocess, os
cmd = ["iwconfig", "wlan0", "|", "grep", "ESSID", "|", "awk", "-F:", "'{print $2}'", "|", "sed", "'s/\"//g'"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = proc.communicate()
print(output.decode("ascii"))
输出应为连接的Wi-Fi的ESSID
很抱歉,我读了其他问题,但我无法理解。
是一个shell功能。您需要shell=True
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
这很奇怪,我得到的输出与终端不同。如果您使用的是
shell=True
,您可能希望将cmd
的元素再次连接到一个字符串中,而不是让Popen
连接它们。很抱歉,我阅读了其他问题,但我无法理解。