June 20th, 2005, 08:36 AM
-
Passing Python Variables To System Calls
Greetings,
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[2]
...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.
Any ideas?
Thanks.
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:
Code:
#!/usr/bin/env python
from subprocess import call
options = '-sP'
hostname = 'hostname'
call(('nmap', options, hostname))
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:
http://forums.devshed.com/search.php?searchid=941730
Hope this helps,
Mark.
July 14th, 2005, 12:21 PM
-