Kill remote session after certain time, if no response from command launched on remote server using Paramiko

前端 未结 1 1126
情歌与酒
情歌与酒 2021-01-16 02:26

I am having some trouble with Paramiko module while running a command on remote server.

def check_linux(cmd, client_ip, client_name):
    port = 22
    usern         


        
相关标签:
1条回答
  • 2021-01-16 02:45

    The .read will wait until the command finishes, what it never does.

    Instead, wait for the command to finish first. If it is taking too long, kill the command (using stdout.channel.close()).

    You can use the code from Python Paramiko exec_command timeout doesn't work?:

    timeout = 30
    import time
    endtime = time.time() + timeout
    while not stdout.channel.eof_received:
        time.sleep(1)
        if time.time() > endtime:
            stdout.channel.close()
            break
    status = stdout.read()
    

    Though you can use the above code, only if the command produces very little output. Otherwise the code might deadlock. For a robust solution, you need to read the output continuously.
    See A command does not finish when executed using Python Paramiko exec_command

    0 讨论(0)
提交回复
热议问题