2

I need to run the following (working) command in Python:

ip route list dev eth0 | awk ' /^default/ {print $3}'

Using subprocess, I would have to do the following:

first = "ip route list dev eth0"
second = "awk ' /^default/ {print $3}'"
p1 = subprocess.Popen(first.split(), stdout=subprocess.PIPE)
p2 = subprocess.Popen(second.split(), stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

Something went wrong with p2. I get:

>>> awk: cmd. line:1: '
awk: cmd. line:1: ^ invalid char ''' in expression

What should I do? On a terminal it works perfectly.

2 Answers 2

16

split splits on any whitespace, including that inside single-quoted arguments. If you really have to, use shlex.split:

import shlex
p2 = subprocess.Popen(shlex.split(second), stdin=p1.stdout, stdout=subprocess.PIPE)

However it usually makes more sense to specify the commands directly:

first = ['ip', 'route', 'list', 'dev', 'eth0']
second = ['awk', ' /^default/ {print $3}']
p1 = subprocess.Popen(first, stdout=subprocess.PIPE)
p2 = subprocess.Popen(second, stdin=p1.stdout, stdout=subprocess.PIPE)
Sign up to request clarification or add additional context in comments.

Comments

4

Not the best solution, but while you are waiting for the best answer, you can still do this :

cmd = "ip route list dev eth0 | awk ' /^default/ {print $3}'"
p2 = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)

3 Comments

Interesting. I thought I had to split the command like I did above. Is there any difference if, like your example, I don't do it?
In this example, you don't have to split the command because shell=True but that's not safe (shell injection)
Drop stdin=subprocess.PIPE. Also output = subprocess.check_output(cmd, shell=True)

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.