title: Using os.forkpty() to create a pseudo-terminal to ssh to a remote server and communicate with it
body: <p>I'm trying to write a python script that can ssh into remote server and can execute simple commands like ls,cd from the python client. However, I'm not able to read the output from the pseudo-terminal after successfully ssh'ing into the server. Could anyone please help me here so that I could execute some commands on the server.</p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>#!/usr/bin/python2.6
  import os,sys,time,thread
  pid,fd = os.forkpty()
  if pid == 0:
      os.execv('/usr/bin/ssh',['/usr/bin/ssh','user@host',])
      sys.exit(0)
  else:
      output = os.read(fd,1024)
      print output
      data = output
      os.write(fd,'password\n')
      time.sleep(1)
      output = os.read(fd,1024)
      print output
      os.write(fd,'ls\n')
      output = os.read(fd,1024)
      print output
  </code></pre>
  
  <p>Sample output:</p>
  
  <pre><code>user@host's password: 
  
  Last login: Wed Aug 24 03:16:57 2011 from 1x.x.x.xxxx
  
  -bash: ulimit: open files: cannot modify limit: Operation not permitted
  host: /home/user&gt;ls
  </code></pre>