python script opening multiple terminals, but only executing command in one

Question:

I’d like my script to open 3 terminals and execute different commands in each, but all it does is run an nmap scan in the original terminal and opens 3 blank terminals.

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)
Asked By: berxiv

||

Answers:

If you wait for the NMAP scan to finish, the whatweb command will start, followed by gobuster in that same terminal.

What you did there was open 3 blank terminals in the for loop, then sequentially run terminal commands in the current terminal.

Just execute 3 subprocess calls.

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]
Answered By: mirmo