June 20th, 2005, 08:36 AM
Passing Python Variables To System Calls
I'm trying to figure out how to control Nmap from Python and am having trouble figuring out how to pass variables into a system call. I'm looking to do something like this:
nmap $options $hostname
...where hostname is something assigned by Python. The issues is that I need to be able to pass Python variables into an os.system call somehow.
So I need to be able to say in Python that:
option = "-sP"
host = sys.argv
...or whatever...and then from there construct an Nmap command that uses those variables.
This has to be easy; I just can't find any documentation on doing it. Tons for Perl -- nothing for Python.
June 20th, 2005, 11:51 AM
If you have Python 2.4 you can use the subprocess module, which is now preferred to using os.system() or the popen2 functions. Here's a simple example of doing this using the call() function:
What this will do is run the command with the options and hostname that follow. You can of course use the os.system() function if you want to as described in these threads:
from subprocess import call
options = '-sP'
hostname = 'hostname'
call(('nmap', options, hostname))
Hope this helps,
July 14th, 2005, 12:21 PM