paramiko python module hangs at stdout.read()

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

    Could be related to https://github.com/paramiko/paramiko/issues/109

    Below is explanation of what i am facing and how i worked around it.

    I also experienced this issue it is due to stdout.channel.eof_received == 0

    import paramiko
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect("1.1.1.1", username="root", password="pass")
    stdin, stdout, stderr = client.exec_command("service XXX start")
    

    stdin, stdout and stderr are staying open...

    >>> print stdin
     >>
    >>> print stdout
     >>
    >>> print stderr
     >>
    

    So EOF was not received...

    >>> print stdin.channel.eof_received
    0
    

    Usually I receive True and can just stdout.read(), but to be safe i use this workaround (which works!): Wait for a timeout, force stdout.channel.close() and then stdout.read():

    >>> timeout = 30
    >>> import time
    >>> endtime = time.time() + timeout
    >>> while not stdout.channel.eof_received:
    ...     sleep(1)
    ...     if time.time() > endtime:
    ...         stdout.channel.close()
    ...         break
    >>> stdout.read()
    'Starting XXX: \n[  OK  ]\rProgram started . . .\n'
    >>>
    

    BTW i use:

    Python 2.6.6
    paramiko (1.15.2)
    

    Hope this helps...

提交回复
热议问题