Sorry to ask this rather off topic question, but: How can I process the output of an external programm I started from within python code?
Up to now I used to use ipac-ng to count the traffic from and to various IPs. Due to the fact that the old distributions shipping ipac-ng are outdated and the program isn’t going to work with newer kernel releases, I am writing my own iptables-script by now. I know that it is easy to let the std-out > file and then read the written file, however, I hope there is an easier way doing this.
I also know that it is an security risk to call an “os” function like that. However, I need IP based traffic over an certain ammount of time. I can’t monitor ports or interfaces because one interface can carry a dozen of IPs.
Currently I am doing it like that:
traffic_cmd="_some_command_stuff_here > /tmp/random_dir/output.txt" os.system(traffic_cmd) traffic_output=open("/tmp/random_dir/output.txt","r") do_something(traffic_output)
Is there no more convenient way to do it? I know that Perl can handle called programs output directly inline. Is there something similar in Python? I mean: I don’t know MUCH about Python. But its beating knowledge about perl many times.
(Plus: the given code has no watchdog timer. If the traffic_cmd never ends, the script also never will. )
Thanks in advance!