title: python paramiko ssh
body: <p>i'm new on python. i wrote a script to connect to a host and execute one command</p>
  
  <pre><code>ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(host, username=user, password=pw)
  
  print 'running remote command'
  
  stdin, stdout, stderr = ssh.exec_command(command)
  stdin.close()
  
  for line in stdout.read().splitlines():
      print '%s$: %s' % (host, line)
      if outfile != None:
          f_outfile.write("%s\n" %line)
  
  for line in stderr.read().splitlines():
      print '%s$: %s' % (host, line + "\n")
      if outfile != None:
          f_outfile.write("%s\n" %line)
  
  ssh.close()
  
  if outfile != None:
      f_outfile.close()
  
  print 'connection to %s closed' %host
  
  except:
     e = sys.exc_info()[1]
     print '%s' %e
  </code></pre>
  
  <p>works fine when then remote command doesn't need a tty. i found an invoke_shell example <a href="https://stackoverflow.com/questions/1911690/nested-ssh-session-with-paramiko">Nested SSH session with Paramiko</a>.  i'm not happy with this solution, because if a server has an prompt that isn't specified in my script -> infinite loop or a specified prompt in the script is a string in the return text -> not all data will be received. is there a better solution maybe where stdout and stderr are send back like in my script?</p>