python脚本打开多个终端,但只在一个中执行命令



我希望我的脚本打开3个终端并在每个终端中执行不同的命令,但它所做的只是在原始终端中运行nmap扫描并打开3个空白终端。

import subprocess
TARGET_IP_ADDRESS = "IPHERE"
terminal_windows = []
for i in range(3):
terminal_windows.append(subprocess.Popen(["gnome-terminal"]))
subprocess.run(["nmap", "-T5", "-A", "-p-", "--min-rate=500", TARGET_IP_ADDRESS], check=True, stdin=terminal_windows[0].stdin)
subprocess.run(["whatweb", TARGET_IP_ADDRESS], check=True, stdin=terminal_windows[1].stdin)
subprocess.run(["gobuster", "dir", "http://" + TARGET_IP_ADDRESS + "/", "--wordlist", "/usr/share/dirb/wordlists/common.txt"], check=True, stdin=terminal_windows[2].stdin)

如果等待NMAP扫描完成,将启动whatweb命令,然后在同一终端中启动gobuster。

你所做的是在for循环中打开3个空白终端,然后依次在当前终端中运行终端命令。

只需执行3个子进程调用。

import subprocess
TARGET_IP_ADDRESS = "IPHERE"
ls_cmd = []
ls_cmd[0] = f"""nmap -TS -A -p- --min-rate=500 {TARGET_IP_ADDRESS}; echo; read -p "Press enter to continue....";"""
ls_cmd[1] = f"""whatweb {TARGET_IP_ADDRESS}; echo; read -p "Press enter to continue....";"""
ls_cmd[2] = f"""gobuster dir http://{TARGET_IP_ADDRESS}/ --wordlist /usr/share/dirb/wordlists/common.txt; echo; read -p "Press enter to continue....";"""
_ = [subprocess.run(f"gnome-terminal -e '{cmd}'", shell=True) for cmd in ls_cmd]

最新更新