0

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,

2
  • 2
    try splitting ""-sP -n 172.16.1.0/24" into separate list elements: "-sP","-n","172.16.1.0/24" Commented Apr 24, 2013 at 21:10
  • 2
    try: check_output(shlex.split("/usr/bin/nmap -sP -n 172.16.1.0/24")) import shlex module first` Commented Apr 24, 2013 at 21:11

3 Answers 3

5
+50

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.

Sign up to request clarification or add additional context in comments.

Comments

1
import subprocess
output = subprocess.check_output(["/usr/bin/nmap", "-sP", "-n", "172.16.1.0/24"])

output = subprocess.check_output(["/usr/bin/nmap", "-sP", "-n", "172.16.1.0/24"], stderr=subprocess.STDOUT)

8 Comments

Thanks, that works, though the actual command I need to get working is : nmap -sn -Pn todd.ns.cloudflare.com --script dns-check-zone --script-args='dns-check-zone.domain=www.macmonster.com' I treid splitting the commands out by no luck.
the principle is the same, each space separated thing should be a list element, you can put the commmand in a string, and the split the sprint and the feed it as input to check_output()
your command does not work on the cmd line for me either, so you must first fix the command
I imagine that the command doesnt work for you as you are not running nmap 6.25
Seems that only part of the nmap output is saved to the variable, but when I check it on the shell its fine and it seems to be sending the output to standard out (??)
|
0

Have you tried this:

p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
var = p.stdout.read()
print var

I would have used communicate, but it returns an odd list-type thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.