2 回答

TA贡献1784条经验 获得超2个赞
试试这个脚本:
#!/usr/bin/env python3
import os
import subprocess
def run_cmd(cmd):
return subprocess.run(cmd, capture_output=True, shell=True).stdout.decode().strip()
def quiet_scan():
address = run_cmd("ip addr | grep 'inet 10.*' | awk '{print $2}'")
print(address)
final = run_cmd(f"nmap -sS {address}")
print(final)
quiet_scan()
该函数run_cmd将 cmd 作为字符串,用 shell 运行它,然后解码结果和最后一个换行符。

TA贡献1795条经验 获得超7个赞
我认为subprocess.run使用capture_outputflag 在这里会很好用:
import subprocess
result = subprocess.run(["nmap", "-sS"], capture_output=True)
print(result.stdout)
result将保存运行进程的结果,包括退出代码和写入流的内容。如果您想要的数据是nmap打印到 的数据stdout,您可以简单地检查该属性。
添加回答
举报