bardgett
bardgett

Reputation: 59

Why are hex digits not passed correctly in python subprocess

I cannot get ping to insert a pattern with python subprocess.Popen... Using 2.6.

Test: open python interpreter...

import subprocess
a = subprocess.Popen(['ping', '-c 1', '-p ff', '172.16.1.1'],subprocess.PIPE)

Result ... ping: patterns must be specified as hex digits.

I have replaced the ff with: "f", "23", 23, 0x23 and a range of things. I just want to see what the shell thinks its getting. But shell=True did not do as I expect, a.communicate() gives (none,none)

Upvotes: 1

Views: 774

Answers (2)

VPfB
VPfB

Reputation: 17292

In addition to answer by Martin Konecny: import shlex and let it do the work for you.

subprocess.Popen(shlex.split('ping -c 1 -p ff 127.0.0.1'), subprocess.PIPE)

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59631

Make sure you split all your "words" into their own list entry:

a = subprocess.Popen(['ping', '-c', '1', '-p' 'ff', '172.16.1.1'],subprocess.PIPE)
# PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

The way you had it, your command was executed as

ping -c\ 1 -p\ ff 172.16.1.1

(the spaces between the args and their value is escaped)

Upvotes: 6

Related Questions