awk von Python: falsch Teilprozess Argumente?

Ich das folgende (Arbeits -) Befehl in Python:

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

Mit subprocess würde ich Folgendes tun:

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]

Etwas schief ging p2. Ich:

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

Was soll ich tun? Auf einem terminal funktioniert es perfekt.

Schreibe einen Kommentar