Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The close() function does not work as expected. #150

Open
jufei opened this issue Aug 25, 2020 · 1 comment
Open

The close() function does not work as expected. #150

jufei opened this issue Aug 25, 2020 · 1 comment

Comments

@jufei
Copy link

jufei commented Aug 25, 2020

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
Copy link
Author

jufei commented Aug 25, 2020

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant