JumpSSH icon indicating copy to clipboard operation
JumpSSH copied to clipboard

The close() function does not work as expected.

Open jufei opened this issue 3 years ago • 1 comments

hi:

I have a cpe device, now I wish to execute reboot and then wait for it up.

my class is inherited from SSHSession of JumpSSH


    def wait_until_cpe_is_up(self, timeout=300, interval=10):
        for i in range(int(int(timeout) / interval)):
            try:
                self.logger.info('{} Try to reopen.'.format(self.name))
                self.open()
                self.logger.info('{} has up.'.format(self.name))
                return True
            except:
                self.logger.info('{} has not yet up.'.format(self.name))
                self.close()
                time.sleep(interval)
        return False


I have set the timeout of my class to 10, that means the open will be timeout=10.

I got the output like this:


[root@Server_130 lib_device]# python3 pet_cpe.py
20-08-25 10:30:21 [ INFO] CPE-860524031814682 ssh port: 10.108.183.134-2201, http_port: 8001, telnet_port: 2301
20-08-25 10:30:21 [ INFO] Exec Command: [reboot]
20-08-25 10:30:23 [ INFO] Exit code: 0, Command output: 
20-08-25 10:30:23 [ INFO] CPE-860524031814682 reboot done.
20-08-25 10:30:23 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:30:33 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:30:43 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:30:53 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:30:53 [ERROR] Exception: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:30:53 [ERROR] Traceback (most recent call last):
20-08-25 10:30:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2211, in _check_banner
20-08-25 10:30:53 [ERROR]     buf = self.packetizer.readline(timeout)
20-08-25 10:30:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 380, in readline
20-08-25 10:30:53 [ERROR]     buf += self._read_timeout(timeout)
20-08-25 10:30:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 607, in _read_timeout
20-08-25 10:30:53 [ERROR]     x = self.__socket.recv(128)
20-08-25 10:30:53 [ERROR] OSError: [Errno 9] Bad file descriptor
20-08-25 10:30:53 [ERROR] 
20-08-25 10:30:53 [ERROR] During handling of the above exception, another exception occurred:
20-08-25 10:30:53 [ERROR] 
20-08-25 10:30:53 [ERROR] Traceback (most recent call last):
20-08-25 10:30:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
20-08-25 10:30:53 [ERROR]     self._check_banner()
20-08-25 10:30:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2216, in _check_banner
20-08-25 10:30:53 [ERROR]     "Error reading SSH protocol banner" + str(e)
20-08-25 10:30:53 [ERROR] paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:30:53 [ERROR] 

20-08-25 10:31:03 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:31:13 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:31:13 [ERROR] Exception: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:13 [ERROR] Traceback (most recent call last):
20-08-25 10:31:13 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2211, in _check_banner
20-08-25 10:31:13 [ERROR]     buf = self.packetizer.readline(timeout)
20-08-25 10:31:13 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 380, in readline
20-08-25 10:31:13 [ERROR]     buf += self._read_timeout(timeout)
20-08-25 10:31:13 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 607, in _read_timeout
20-08-25 10:31:13 [ERROR]     x = self.__socket.recv(128)
20-08-25 10:31:13 [ERROR] OSError: [Errno 9] Bad file descriptor
20-08-25 10:31:13 [ERROR] 
20-08-25 10:31:13 [ERROR] During handling of the above exception, another exception occurred:
20-08-25 10:31:13 [ERROR] 
20-08-25 10:31:13 [ERROR] Traceback (most recent call last):
20-08-25 10:31:13 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
20-08-25 10:31:13 [ERROR]     self._check_banner()
20-08-25 10:31:13 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2216, in _check_banner
20-08-25 10:31:13 [ERROR]     "Error reading SSH protocol banner" + str(e)
20-08-25 10:31:13 [ERROR] paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:13 [ERROR] 
20-08-25 10:31:23 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:31:33 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:31:33 [ERROR] Exception: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:33 [ERROR] Traceback (most recent call last):
20-08-25 10:31:33 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2211, in _check_banner
20-08-25 10:31:33 [ERROR]     buf = self.packetizer.readline(timeout)
20-08-25 10:31:33 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 380, in readline
20-08-25 10:31:33 [ERROR]     buf += self._read_timeout(timeout)
20-08-25 10:31:33 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 607, in _read_timeout
20-08-25 10:31:33 [ERROR]     x = self.__socket.recv(128)
20-08-25 10:31:33 [ERROR] OSError: [Errno 9] Bad file descriptor
20-08-25 10:31:33 [ERROR] 
20-08-25 10:31:33 [ERROR] During handling of the above exception, another exception occurred:
20-08-25 10:31:33 [ERROR] 
20-08-25 10:31:33 [ERROR] Traceback (most recent call last):
20-08-25 10:31:33 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
20-08-25 10:31:33 [ERROR]     self._check_banner()
20-08-25 10:31:33 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2216, in _check_banner
20-08-25 10:31:33 [ERROR]     "Error reading SSH protocol banner" + str(e)
20-08-25 10:31:33 [ERROR] paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:33 [ERROR] 
20-08-25 10:31:43 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:31:53 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:31:53 [ERROR] Exception: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:53 [ERROR] Traceback (most recent call last):
20-08-25 10:31:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2211, in _check_banner
20-08-25 10:31:53 [ERROR]     buf = self.packetizer.readline(timeout)
20-08-25 10:31:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 380, in readline
20-08-25 10:31:53 [ERROR]     buf += self._read_timeout(timeout)
20-08-25 10:31:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 607, in _read_timeout
20-08-25 10:31:53 [ERROR]     x = self.__socket.recv(128)
20-08-25 10:31:53 [ERROR] OSError: [Errno 9] Bad file descriptor
20-08-25 10:31:53 [ERROR] 
20-08-25 10:31:53 [ERROR] During handling of the above exception, another exception occurred:
20-08-25 10:31:53 [ERROR] 
20-08-25 10:31:53 [ERROR] Traceback (most recent call last):
20-08-25 10:31:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
20-08-25 10:31:53 [ERROR]     self._check_banner()
20-08-25 10:31:53 [ERROR]   File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2216, in _check_banner
20-08-25 10:31:53 [ERROR]     "Error reading SSH protocol banner" + str(e)
20-08-25 10:31:53 [ERROR] paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 9] Bad file descriptor
20-08-25 10:31:53 [ERROR] 
20-08-25 10:32:03 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:32:09 [ INFO] CPE-860524031814682 has up.
[root@Server_130 lib_device]# 
[root@Server_130 lib_device]# 

Though at last the device is up, but I got so many error info.

I have tried to remove the close() like:

            except:
                self.logger.info('{} has not yet up.'.format(self.name))
                time.sleep(interval)

I got the output with no error, but in fact, the device is not up at all and the code has return:

[root@Server_130 lib_device]# python3 pet_cpe.py
20-08-25 10:33:55 [ INFO] CPE-860524031814682 ssh port: 10.108.183.134-2201, http_port: 8001, telnet_port: 2301
20-08-25 10:33:55 [ INFO] Exec Command: [reboot]
20-08-25 10:33:57 [ INFO] Exit code: 0, Command output: 
20-08-25 10:33:57 [ INFO] CPE-860524031814682 reboot done.
20-08-25 10:33:57 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:34:07 [ INFO] CPE-860524031814682 has not yet up.
20-08-25 10:34:17 [ INFO] CPE-860524031814682 Try to reopen.
20-08-25 10:34:17 [ INFO] CPE-860524031814682 has up.
[root@Server_130 lib_device]# 

jufei avatar Aug 25 '20 02:08 jufei

BTW, I have added closed() in the reboot function, it is like:

    def reboot(self):
        self.exec_command('reboot', timeout=10)
        self.close()
        self.logger.info('{} reboot done.'.format(self.name))

jufei avatar Aug 25 '20 02:08 jufei