How to know if a paramiko SSH channel is disconnected?

前端 未结 4 1491
闹比i
闹比i 2020-12-31 08:53

I\'m desinging test cases in which I use paramiko for SSH connections. Test cases usually contain paramiko.exec_command() calls which I have a wrapper for (call

4条回答
  •  耶瑟儿~
    2020-12-31 09:42

    This works:

    import paramiko
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())        # Setting the missing host policy to auto add it
    client.connect('192.168.1.16', port=22, username='admin', password='admin', timeout=3, banner_timeout=2)
    
    channel = client.invoke_shell()                 # Request an interactive shell session on this channel. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the shell.
    print channel.closed          # False
    command = 'reboot'
    channel.send(command + '\n')
    # wait a while
    print channel.closed          # True
    

提交回复
热议问题