python - 如何将来自`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)
请原谅我,如果这很简单,我对编码来说还算新,但总的来说,但我到处检查过,似乎找不到答案
解决方案
您可以使用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
打印的任何内容。
推荐阅读
- multithreading - 并发线程、进程和多核
- php - 使用 swiftmailer 发送电子邮件时出现解析错误
- java - 如何避免在后台运行时启动应用程序时显示启动画面?
- python - 如何使用 For 循环语句从 4 个数据帧创建一个相似列的数据帧
- javascript - 向前和向后遍历树状结构
- json - 在 Angular 中解析巨大的 XML 文件的超时或策略问题
- python - 在 matplotlib 上绘制一个 numpy 数组
- javascript - Chrome 扩展程序不会在按钮单击时运行 .JS
- concatenation - 在 Qlik Sense 中加载多个表格时出错
- java - Spring boot 启动Exception ServerPropertiesAutoConfiguration.class] 无法打开,因为它不存在