Skip to content

Instantly share code, notes, and snippets.

@ralt
Created May 7, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralt/ac2ca80006290f150a8f99c43f21f142 to your computer and use it in GitHub Desktop.
Save ralt/ac2ca80006290f150a8f99c43f21f142 to your computer and use it in GitHub Desktop.
Paramiko interactive subprocess
# 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