Python script - launch nmap with parameters

315 Views Asked by At

The scripts launches the application nmap. Only it doesn't print out the var to the application nmap. It does however if I print them separately.

Looking for some suggestions, many thanks.

Image script - Image output

Script:

import os
import subprocess

xIP=input("Please enter IP-address to use on NMAP port-scan: ")
xOP=input("Please apply options to add on port-scan: ")

print("Entered values: ", xIP + xOP)

command = "nmap print xIP xOP"
#os.system(command)
subprocess.Popen(command)

input("Press ENTER to exit application...")
2

There are 2 best solutions below

0
On BEST ANSWER

See here: https://docs.python.org/3/library/subprocess.html#subprocess.Popen

Split the program name and the arguments, the first item in the list it expects is the name of the program to run.

In your case, subprocess.Popen(command.split(' ')) may suffice.

0
On
command = "nmap print xIP xOP"

This line is the issue. First, as Uriya Harpeness mentioned, subprocess.Popen() works best when the command is split into pieces in a collection like a list. Instead of using str.split() as they suggest, shlex.split() is designed for this, as the example in the Popen docs shows.

However, the other issue you're having is that you've placed your variable names into a string literal, so they're just being treated as part of the string instead of being evaluated for their contents. To get the behavior you want, just reference the variables directly:

command = ["nmap", "print", xIP, xOP]
subprocess.Popen(command)