#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0

    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.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    You rock, dude.

IMN logo majestic logo threadwatch logo seochat tools logo