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

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0

    Passing Python Variables Into System Calls


    I'm trying to call Nmap from within Python and pass it variables that I've created, and I can't find any good documentation or examples of how to do it.

    Intuition tells me it'd be something like:

    Code:
    os.popen ("nmap -p1-'ports'")
    ...in order to pass the upper port limit to Nmap (where I've defined ports within Python). I'm thinking there has to be a method of adding in Python variables within these strings so that the Python variables are substituted and then sent to the system call.

    Thoughts?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    with python 2.3 and lower you can do this:

    Code:
    >>> import os
    >>> os.system("echo hey")
    hey
    0
    >>>
    for python 2.4 and newer you should use the sub process method, however it is only on 2.4. I beleave the main differance is that subprocess just gives you more controle, it lets you handle all the stuff like return codes and outputs. Lets you pipe and everything else. Where as system method only prints output and such.

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

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    Thanks for the post.

    How would I call Popen (one of the subprocesses)? When I try like in the documentation it says there's no such thing. It seems I need to import something first.

    Anyhow, anyone know how to pass variables into os.system calls?
  6. #4
  7. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    same way you add them to any string:

    Code:
    os.system ( "ls "+ VariableDirectory );
    note the space after the "ls"
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by danielrm26
    Thanks for the post.

    How would I call Popen (one of the subprocesses)? When I try like in the documentation it says there's no such thing. It seems I need to import something first.

    Anyhow, anyone know how to pass variables into os.system calls?
    In the documentation Popen is imported explicitly using the from-import syntax. If you want to use this in your program then you either need to import it in this way or fully qualify it i.e. subprocess.Popen()

    Code:
    >>> import subprocess
    >>> subprocess.Popen
    <class 'subprocess.Popen'>
    >>>
    >>> from subprocess import Popen
    >>> Popen
    <class 'subprocess.Popen'>
    >>>
    You might also consider using the call() function, depending on what you need to do with it for simplicity .

    Hope this helps,

    Mark.
    Last edited by netytan; June 7th, 2005 at 04:53 PM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo