How to know if a paramiko SSH channel is disconnected?

前端 未结 4 1468
闹比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:32

    My solution basically the same as yours, just organized differently:

    def connection(self):
        if not self.is_connected():
            self._ssh = paramiko.SSHClient()
            self._ssh.connect(self.server, self.port,
                              username = self.username, password = self.password)
    
        return self._ssh
    
    def is_connected(self):
        transport = self._ssh.get_transport() if self._ssh else None
        return transport and transport.is_active()
    
    def do_something(self):
        self.connection().exec_command('ls')
    

提交回复
热议问题