Hi I am trying to get output of
airodump-ng mon0
where mon0 is monitor mode of my wireless interface. I want to read the output of the command continuously without killing the process in certain time interval
My code is as follow:
import subprocess
import time
airodump = subprocess.Popen(['airodump-ng','mon0'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
for i in [1, 2, 3, 4 ,5]:
time.sleep(10)
o_file = airodump.stdout
x = o_file.read()
print(x)
airodump.kill()
It seems like program gets stuck at x = o_file.read(). Please help.
When setting both
stdout=subprocess.PIPEandstderr=subprocess.PIPEthere's a risk of deadlock (unless you usecommunicateto read both channels together), because if the process writes on standard error and you're trying to read standard output, both block forever.In your case, you want to control the reading, so
communicateisn't an option. I suspect that you just want to merge both streams so change:by
to redirect standard error to standard output and get all output+error in
o_file.stdoutAside:
for i in [1, 2, 3, 4 ,5]:would be more "pythonic" like:for _ in range(5):since you're not usingi, and also imagine that you want to loop 10000 times :)But that doesn't solve your problem if your application isn't printing the lines at once in all cases, because
read()is blocking, and you need it to stop exactly when you want, so I would:True& terminate the processLike this: