-
Notifications
You must be signed in to change notification settings - Fork 1
/
shellinteract.py
40 lines (33 loc) · 1.26 KB
/
shellinteract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import subprocess
from queue import Queue
from threading import Thread
class InteractiveShell:
def __init__(self, command, PS1, logfile):
def _enqueue_output(out, queue, out_file):
for line in iter(out.readline, b''):
out_file.write(line)
queue.put(line)
out.close()
self.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=False, universal_newlines=True)
self.outQueue = Queue()
self.outThread = Thread(target=_enqueue_output, args=(self.process.stdout, self.outQueue, logfile))
self.outThread.daemon = True
self.outThread.start()
self.PS1 = PS1
def execute(self, command):
self.process.stdin.write(command + '\n')
def get_output(self):
output = ''
while self.PS1 not in output:
self.outQueue.get()
return output
def execute_and_get_output(self, command):
self.execute(command)
return self.get_output()
def stop(self, timeout=0.2):
self.execute('exit')
try:
self.process.wait(timeout)
except subprocess.TimeoutExpired:
self.process.kill()