首页 > 解决方案 > 如何将来自`os.system`调用的标准输出存储到变量中?

问题描述

所以最近我一直在致力于自动化我的错误赏金代码,但我想有一个整体的输出,这样它就可以清楚地显示它得到了什么

(例如这里是 xssstrike)

website = (input(Fore.GREEN + "enter url/website for this scan: "))
ops = (input(Fore.GREEN + "enter other operators for xxstrike here (with spaces): "))

def xssstrike():
    try:
        os.chdir("/mnt/usb/xss-strike")
        os.system(f"python3 xsstrike.py {ops}  -u {website}")
    except ValueError:
           raise print("oops! there was an error with xss strike!")

我想将输出os.system(f"python3 xsstrike.py {ops} -u {website}")放入一个变量中,以便稍后在代码末尾打印它,例如

print("<><><> xss strike output results <><><>")
print(xssstrikeoutput)

请原谅我,如果这很简单,我对编码来说还算新,但总的来说,但我到处检查过,似乎找不到答案

标签: pythonpython-3.x

解决方案


您可以使用subprocess.check_output内置subprocess模块执行此操作

import subprocess


# instead of os.system
xssstrikeoutput_bytes: bytes = subprocess.check_output(f"python3 xsstrike.py {ops}  -u {website}", shell=True)
xssstrikeoutput = xssstrikeoutput_bytes.decode("utf-8")

这样,您将能够看到您xssstrike.py打印的任何内容。

subprocess.check_output文件


推荐阅读