为什么subprocess.check_output会产生CalledProcessError错误?

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

我尝试了很多不同的方法,但我不能解决这个问题......我不是python专家,任何人都可以解释如何解决这个问题......plzzz。

command = "netsh wlan show profile"
ssid = subprocess.check_output(command, shell=True)
ssid = ssid.decode("utf-8")
ssid_list = re.findall('(?:Profile\s*:\s)(.*)', ssid)
for ssid_name in ssid_list:
    subprocess.check_output(["netsh","wlan","show","profile",ssid_name,"key=clear"])

它给出了错误。

Traceback (most recent call last):
  File "C:/Users/Prakash/Desktop/Hacking tool/Execute_sys_cmd_payload.py", line 18, in <module>
    subprocess.check_output(["netsh","wlan","show","profile",ssid_name,"key=clear"])
  File "C:\Users\Prakash\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "C:\Users\Prakash\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['netsh', 'wlan', 'show', 'profile', 'Prakash_WiFi\r', 'key=clear']' returned non-zero exit status 1.

Process finished with exit code 1
python-3.x subprocess output check
1个回答
0
投票

check_output 每当一个命令异常时,这个异常就会被引发,如非零的返回代码所示。

一个可能的解决方案是使用 subprocess.run 代替。这将运行该进程并返回一个 CompletedProcess 实例,它有一个 returncode 属性,你可以检查。

import subprocess as sp

command = ["netsh","wlan","show",ssid_name,"key=clear"]
netsh = subprocess.run(command, shell=True, stdout=sp.PIPE)
if netsh.returncode != 0:
    print(f'netsh error: {netsh.returncode}')
else:
   # process netsh.stdout.

从阅读 本页看来,你应该使用的命令是 netsh wlan show (无轮廓)。

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