0

I tried many methods described but its not working with me. Can anybody please explain how can I use this in a single python script using the subprocess?

iperf -c 10.0.0.1 -i 1 -t 100 | grep -Po '[0-9.]*(?= Mbits/sec)'

Amigo
  • 41
  • 6
  • Piping from one command to another is a shell feature, so you *must* specify `shell=True` on the subprocess method you use. But why not just iterate over the output in Python and forego the unnecessary `grep`? – kindall Mar 08 '17 at 23:02
  • What didn't work? How didn't it work? – Peter Wood Mar 08 '17 at 23:06
  • bes = subprocess.Popen(["iperf", "-c", 10.0.1.1 | "grep","-Po", [0-9.]*(?= Mbits/sec)'], shell=True, stdout=subprocess.PIPE).communicate()[0] – Amigo Mar 08 '17 at 23:10
  • This is how I am using it. Its not working – Amigo Mar 08 '17 at 23:11

1 Answers1

0

So I have resolved the problem. The idea is to use both the commands with different sub-processes. First create the process for iperf and input the out of this process to the stdin of the second process of the grep command as below:

process1 = subprocess.Popen(["iperf","-c", 10.10.0.1],  stdout=subprocess.PIPE)

prrocess2 = subprocess.Popen(["grep", "-Po",[0-9.]*(?= Mbits/sec)], stdin=process1.stdout, stdout=subprocess.PIPE).communicate()[0] 
Amigo
  • 41
  • 6