netmiko: Handling exception when SSH is not enabled on Cisco devices.
My code:
try:
net_connect = ConnectHandler(**cisco_1)
except SSHException:
print('SSH is not enabled for this device.')
sys.exit()
When a Cisco device does not have SSH enabled, a SSHException exception will be thrown by Paramiko as shown in the stacktrace below.
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1867, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 327, in readline
buf += self._read_timeout(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 483, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1723, in run
self._check_banner()
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1871, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1867, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 327, in readline
buf += self._read_timeout(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 483, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\UserAdm\PycharmProjects\CiscoPL2\dirconnect.py", line 18, in <module>
net_connect = ConnectHandler(**cisco_1)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\netmiko\ssh_dispatcher.py", line 88, in ConnectHandler
return ConnectionClass(*args, **kwargs)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\netmiko\base_connection.py", line 68, in __init__
self.establish_connection(verbose=verbose, use_keys=use_keys, key_file=key_file)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\netmiko\base_connection.py", line 165, in establish_connection
self.remote_conn_pre.connect(**ssh_connect_params)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\client.py", line 338, in connect
t.start_client()
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 493, in start_client
raise e
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1723, in run
self._check_banner()
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1871, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
I tried catching the SSHException, but it will not catch all the exceptions, which I think is not the expected behavior ? Stacktrace below.
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1867, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 327, in readline
buf += self._read_timeout(timeout)
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\packet.py", line 483, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1723, in run
self._check_banner()
File "C:\Users\UserAdm\AppData\Local\Programs\Python\Python35\lib\site-packages\paramiko\transport.py", line 1871, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
SSH is not enabled for this device.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 19 (9 by maintainers)
@TheOtherGuy if you want to eliminate the Traceback out put. just set the tracebacklimit to 0 import sys sys.tracebacklimit = 0
Normal Output
Connecting to device" 192.168.200.201 Exception: Error reading SSH protocol banner Traceback (most recent call last): File “/home/dev/devnet/lib/python3.8/site-packages/paramiko/transport.py”, line 2211, in _check_banner buf = self.packetizer.readline(timeout) File “/home/dev/devnet/lib/python3.8/site-packages/paramiko/packet.py”, line 380, in readline buf += self._read_timeout(timeout) File “/home/dev/devnet/lib/python3.8/site-packages/paramiko/packet.py”, line 609, in _read_timeout raise EOFError() EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File “/home/dev/devnet/lib/python3.8/site-packages/paramiko/transport.py”, line 2039, in run self._check_banner() File “/home/dev/devnet/lib/python3.8/site-packages/paramiko/transport.py”, line 2215, in _check_banner raise SSHException( paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
SSH Issue. Are you sure SSH is enabled? 192.168.200.201 Connecting to device" 192.168.200.202 configure terminal Enter configuration commands, one per line. End with CNTL/Z. vios2(config)#ntp server 10.10.10.10 vios2(config)#ntp server 10.10.100.110 vios2(config)#end vios2#
after the tracebacklimit to 0
Connecting to device" 192.168.200.201 Exception: Error reading SSH protocol banner Traceback (most recent call last): EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
SSH Issue. Are you sure SSH is enabled? 192.168.200.201 Connecting to device" 192.168.200.202 configure terminal Enter configuration commands, one per line. End with CNTL/Z. vios2(config)#ntp server 10.10.10.10 vios2(config)#ntp server 10.10.100.110 vios2(config)#end vios2#
@TheOtherGuy It looks like it is probably this issue:
https://github.com/paramiko/paramiko/issues/643
Basically a background thread is encountering an exception and printed out the exception message (which it sounds like it is hard to prevent the exception message). In the main thread the exception is handled (see the printing of ‘ssh not enabled message’).
So at a high level what are you trying to do. In other words, can you give me a broader context on what you are trying to accomplish.
Also what version of Paramiko are you using?
Kirk
@ktbyers
This is my exact code which I use for testing:
I did actually get the expected behavior as described (handled successfully by NetmikoTimeoutException), but it is not consistent. Out of the 10 times I tried running the same code, I got the expected behavior 4/10 times. The other 6 times resulted in the error I posted in https://github.com/ktbyers/netmiko/issues/228#issuecomment-228330622