Search code examples
pythongrepsubprocessiperf

How can I use iperf and grep both in a python script?


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)'


Solution

  • 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]