paramiko python module hangs at stdout.read()

前端 未结 4 1579
北恋
北恋 2021-01-04 06:24

I am using the below code:

import paramiko

def runSshCmd(hostname, username, password, cmd, timeout=None):          
    client = paramiko.SSHClient()
            


        
4条回答
  •  情话喂你
    2021-01-04 06:56

    It use to happen when there is no data in stdout or there is a line without eol (i.e. in a read statement inside a sh script). Try setting 'get_pty=True', then reading only the bytes in stdout. To avoid infinite loops, it'd be a good idea setting a timeout and a sleep in spite of the continue statement:

    stdin, stdout, stderr = ssh.exec_command("your-command",get_pty=True)
    stdout.flush()
    nbytes = 0
    while (len(stdout.channel.in_buffer)==0):
         continue
    
    nbytes=len(stdout.channel.in_buffer)
    print(nbytes)
    stdout.read(nbytes)
    

提交回复
热议问题