Paramiko: bloques de tuberías para siempre en lectura

I have a problem with getting piping to work with paramiko.

Esto funciona:

ssh = paramiko.SSHClient()
[...]
stdin, stdout, stderr = ssh.exec_command("find /tmp")
stdout.read()

Esto hace no work (blocks forever on stdout.read()):

[...]
stdin, stdout, stderr = ssh.exec_command("bash -")
stdin.write("find /tmp\n")
stdin.close()
stdout.read()

¿Alguna idea?

EDIT:

I looked at the source code for paramiko, and ChannelFile.close does not really do anything in terms of communication. So I looked at the channel API, and this seems to work:

stdin.write("find /tmp\n")
stdin.flush()
stdin.channel.shutdown_write()
stdout.read()

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

With some investigation, it appears that stdin.close() does not actually end the bash session. To do that, you could use the bash command exit (stdin.write('exit\n')) or dig into the paramiko Channel object underneath the stdin :

stdin.channel.shutdown_write()

If you'd like the bash session to continue for another command, you'll need to use the channel object directly. The documentación para Channel menciona recv_ready(self) y recv(self, nbytes) which will allow you t check for the data before you try to get it.

respondido 09 nov., 11:02

Yeah, I figured it out myself already, but SO does not let me answer my own qestions yet :-) - HMN

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.