1

I'd like to open a long-running connection with paramiko that requires bi-directional communication. How can I get selectable stdin, stdout and stderr file descriptors? The following:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="localhost", username="xyz")

stdin, stdout, stderr = ssh.exec_command(cmd)

print "STDIN  FILENO", stdin.channel.fileno()
print "STDOUT FILENO", stdout.channel.fileno()
print "STDERR FILENO", stderr.channel.fileno()

... prints "6" for each file descriptor, which turns out to be the stdout of the connection.

But I'd like to do:

r,w,e = select.select( [stdoutfd, stderrfd], [stdinfd]  )
user48956
  • 14,850
  • 19
  • 93
  • 154

1 Answers1

0

stdin, stdout, stderr are file-like objects. Use things like stdout.readlines() to get the data. If you read the underlying channel object, you see that its all implemented on one connection.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Thanks. Have updated the question. read/readlines are not what I'm looking for. I'd like to use select on file descriptors to efficiently manage a long running connection. – user48956 Mar 30 '15 at 15:37
  • 1
    That's a problem for paramiko. It implements the ssh protocol on top of the socket using threads so its difficult to do anything useful with select. Maybe something like twisted's ssh implementation would work for you. I don't know the native libssh implementation, but that may be another avenue. – tdelaney Mar 30 '15 at 16:03
  • http://stackoverflow.com/questions/760978/long-running-ssh-commands-in-python-paramiko-module-and-how-to-end-them ... suggests that I should be able to use select. However, the examples I find only work with stdout. – user48956 Mar 30 '15 at 18:40