Created
May 7, 2016 20:31
-
-
Save ralt/ac2ca80006290f150a8f99c43f21f142 to your computer and use it in GitHub Desktop.
Paramiko interactive subprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In the paramiko server... this is super ugly. | |
from subprocess import Popen, PIPE | |
import select | |
def _read_chan(chan, stdin): | |
x = chan.recv(1) | |
if ord(x) == 13: | |
chan.send("\r\n") | |
stdin.write("\n") | |
else: | |
chan.send(x) | |
stdin.write(x) | |
stdin.flush() | |
def _write_chan(chan, out): | |
x = out.read(1) | |
chan.send(x.replace("\n", "\r\n")) | |
s = Popen(["python", "-i"], stdout=PIPE, stdin=PIPE, stderr=PIPE) | |
while True: | |
r, w, e = select.select([chan, s.stdout, s.stderr], [], []) | |
if chan in r: | |
_read_chan(chan, s.stdin) | |
if s.stdout in r: | |
_write_chan(chan, s.stdout) | |
if s.stderr in r: | |
_write_chan(chan, s.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment