'I have an issue with Netmiko Error reading SSH protocol banner

I wrote a code to go through an inventory list of Cisco devices and do some tasks. My problem is because the inventory list is huge, and no guarantee if the device is online or decommissioned, i have to run the code anyway and expect that connection error.

I noticed that when i connect to the device without proxy, it doesn't show this banner error for the offline device.

I'm using Netmiko, but here i'm stuck with this error when the device is offline:

               check1= 0

            try:        #Avoid Timeout & Auth errors and continuo for next switch
                net_connect = ConnectHandler(**switch)
            except (NetMikoTimeoutException, NetMikoAuthenticationException):
                print ('\n' + 'Cannot connect to device: ' + HOST)
                check1= 1
            except paramiko.AuthenticationException:
                print ('\n' + 'AuthenticationException ' + HOST)
                check1= 1
            except paramiko.SSHException as e:
                print ('\n' + 'SSHException' + HOST)
                check1= 1

            except paramiko.ssh_exception.SSHException as e:
                if e.message == 'Error reading SSH protocol banner':
                    print ('\n' + 'SSHException' + HOST)
                check1= 1

            except paramiko.ssh_exception.NoValidConnectionsError as e:
                if e.message == 'Error reading SSH protocol banner':
                    print ('\n' + 'SSHException' + HOST)
                check1= 1

            
            if bool(check1) == False:   #If can connect, then continue... otherwise, skip to next host in the for loop

Then the code when it comes to an offline device, it will give this error:

    Exception (client): Error reading SSH protocol banner
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2270, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 380, in readline
    buf += self._read_timeout(timeout)
  File "/usr/local/lib/python3.6/site-packages/paramiko/packet.py", line 622, in _read_timeout
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2093, in run
    self._check_banner()
  File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 2275, in _check_banner
    "Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source