paramiko python module hangs at stdout.read()

前端 未结 4 1577
北恋
北恋 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:41

    I happen to come across this issue. But I kinda work around it by using "readline" instead "readlines".

    For example:

    client = paramiko.SSHClient()
    client.connect(addr, port, username, password)
    stdin, stdout, stderr = client.exec_command(cmd)
    
    while True:
        print(stdout.readline())
        if stdout.channel.exit_status_ready():
            break
    

    So it will print every line immediately and no more hanging, also exit_status_ready() will make sure the loop breaks when stdout has stopped/exited.

提交回复
热议问题