Python popen() with paramiko interactive.py
<p>Hi I'm newbie in python.</p>

<p>I am now developing detaching ssh shell using popen() method.</p>

<pre><code>"Start a shell process for running commands"
     if self.shell:
         error( "%s: shell is already running" )
         return
      cmd = [ './sshconn.py' ]
      self.shell = Popen( cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
            close_fds=True )

      self.stdin = self.shell.stdin
      self.stdout = self.shell.stdout
      self.pid = self.shell.pid
      self.pollOut = select.poll()
      self.pollOut.register( self.stdout )
</code></pre>

<p>And this method uses interactive.py code in paramiko's demo as command.</p>

<pre><code>#!/usr/bin/python

import sys
import paramiko
import select
import termios
import tty

def main():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('host', username='user', password='secret')

    tran = ssh.get_transport()
    chan = tran.open_session()

    chan.get_pty()
    chan.invoke_shell()

    oldtty = termios.tcgetattr(sys.stdin)
    try:
            while True:
                    r, w, e = select.select([chan, sys.stdin], [], [])
                    if chan in r:
                            try:
                                    x = chan.recv(1024)
                                    if len(x) == 0:
                                            print '\r\n*** EOF\r\n',
                                            break
                                    sys.stdout.write(x)
                                    sys.stdout.flush()
                            except socket.timeout:
                                    pass
                    if sys.stdin in r:
                            x = sys.stdin.read(1)
                            if len(x) == 0:
                                    break
                            chan.send(x)
    finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)

if __name__ == '__main__':
    main()
</code></pre>

<p>The problem is when popen() is executed, it returns 
Traceback (most recent call last):</p>

<pre><code>File "./sshconn.py", line 43, in &lt;module&gt;
    main()
File "./sshconn.py", line 20, in main
    oldtty = termios.tcgetattr(sys.stdin)
    termios.error: (22, 'Invalid argument')
</code></pre>

<p>How can I solve this?</p>
