0

Have been needing to write some code that recognises (and captures) when there's incoming/outgoing smb transfers on my server. I think the best way is to use popen with 'iftop'.

I was thinking I could just use popen to capture the data, but thing is iftop doesn't simply output the data - its an interactive environment that runs continuously until the user exits. So how can I parse values from these kind of programs into python?

Alternatively - a better option that popen(iftop) ?

Example:

foo.Popen("iftop" + someArguments)
//this is not stdout, so I can't pull string values (etc) from it :(
//how does?
user3063850
  • 281
  • 1
  • 2
  • 7

1 Answers1

0
import subprocess, time
s = subprocess.Popen("iftop -s -t 1",shell=True,stdout=subprocess.PIPE)
while not s.poll() == 0:
    time.sleep(1)
print(s.stdout.read())
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • This should be `iftop -s 1 -t` (the -s flag requires the parameter to come immediately after it). When there is no number following `-s`, iftop shows "unknown option -s". – Zane Claes Apr 03 '20 at 15:16