python subprocess issue with Nmap
Question: Im trying to get a linux binary to send its standard output to a variable by
using subprocess. But just keep getting tracebacks.

    
    
    >>> import subprocess
    >>>nmap -sn -Pn todd.ns.cloudflare.com --script dns-check-zone --script-args='dns-check-zone.domain=www.macmonster.com
    

Any Ideas (oh and Im using Python2.7).

Ideally I would like to avoid using Shell=true to avoid any security concerns.

Thanks,


Answer: ## shlex to the rescue!

The module shlex will take a string containing the whole shell command and
split it up exactly how Popen and check_output expect it. Like this:

    
    
    import shlex, subprocess
    cmd = "/usr/bin/nmap -sn -Pn todd.ns.cloudflare.com --script dns-check-zone --script-args='dns-check-zone.domain=www.macmonster.com'"
    args = shlex.split(cmd)
    output = subprocess.check_output(args)
    

When you look at contents of args you'll see:

    
    
    >>> print args
    ['/usr/bin/nmap', '-sn', '-Pn', 'todd.ns.cloudflare.com', '--script', 'dns-check-zone', '--script-args=dns-check-zone.domain=www.macmonster.com']
    

Note that shlex split up the option "--script dns-check-zone" into two tokens.
On the other hand it kept "--script-args='dns-check-
zone.domain=www.macmonster.com'", but removed the single-quotes.

