-2

Is it possible to extract the live output from a command running in a subprocess ?

I want to use the output of a program called wifite (https://github.com/derv82/wifite) in my Script. Wifite should be run in a subprocess in my script - at a specific time wifite will output it´s scan results and update them every second (or something near that). I want to have this line of output while it runs to display on my Raspberry Pi Adafruit LCD.

So I want to do something like this:

wifite_scan = subprocess.Popen('./wifite.py', shell=True, stdout =PIPE)
wfite_scanOut= wifite_scan.communicate()
lcd.message(wifite_scanOut)

But this won't work (correct me if I´m wrong) live.

...To get the last line of this Output live on my lcd:


1  Example1               7  WPA2  58db   wps 
2  Example2               6  WPA2  47db    no 
3  Example4               7  WPA2  47db    no 
4  Example5               1  WPA2  31db    no 

[0:00:11] scanning wireless networks. 4 targets and 0 clients found

This output updates every 5 seconds, and just posts the same output with new values in the console. So I need a way to get the last line every 5 seconds in an variable.

Whats the best way to achieve live output to the lcd ?

Nroh
  • 11
  • 9
  • 1
    Welcome to Stackoverlfow! you will greatly increase your chances of getting an answer for your question if you include your input, what you have tried, your expected output vs. your actual output and the full stack trace of any errors you receive. You can also read [this guide](http://stackoverflow.com/help/mcve) – kylieCatt Jun 23 '15 at 16:32
  • Possible duplicate of http://stackoverflow.com/questions/1996518/retrieving-the-output-of-subprocess-call – cdarke Jun 23 '15 at 16:33
  • Not a duplicate. I want to get the live output I would see if I ran ./wifite.py in a normal console. – Nroh Jun 23 '15 at 17:06

2 Answers2

0

Ever tried pexpect ? I have successfully used it with various software.

Example - use the mysql client to count the no of tables of test (must have at least one table to not break) :

child = pexpect.spawn('mysql')
child.expect('mysql>')
child.sendline('use test; show tables;')
child.expect('(\d+) row.* in set \(.*\)')
count_tables = child.match.groups()[0]

After a call to expect(expr) you can inspect the contents of the buffer with child.before and child.buffer.

  • Could u give me a hint on how u can extract output from one of those child processes ? All I see in the documentation is that u can control programs over python with it. – Nroh Jun 24 '15 at 19:59
  • Added a quick example. –  Jun 26 '15 at 15:52
0

i'm not sure if it suits your problem (because i don't know what the output of the subscript looks like), but this could be a solution:

import subprocess

wifite_scan = subprocess.Popen('wifite.py', shell=True, stdout=subprocess.PIPE)
while True:
    output = wifite_scan.stdout.readline()
    if output == '':
        # end of stream
        print("End of Stream")
        break
    if output is not None:
        # here you could do whatever you want with the output.
        print("Output Read: "+output)

i tried the script above with a simple file that generated some output:

# wifite.py
import time

for i in range(10):
    print(i)
    time.sleep(1)

worked for me.

Felix
  • 6,131
  • 4
  • 24
  • 44