Handling exception when SSH is not enabled on Cisco devices.
See original GitHub issueMy 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.
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (9 by maintainers)
Top Results From Across the Web
Improving exception handling of Netmiko script to SSH into ...
I'm trying to improve the exception handling of a netmiko script that successfully runs commands on Cisco devices listed in a text file...
Read more >Configure SSH on Routers and Switches - Cisco
This document describes how to configure and debug Secure Shell (SSH) on Cisco routers or switches that run Cisco IOS® Software.
Read more >Configure Secure Shell (SSH) Server Authentication Settings ...
This article provides instructions on how to configure server authentication on a managed switch.
Read more >Solved: SSH problem - Cisco Community
I have 4 routers connected to a switch all have ssh configured and I am able to reach them all from the switch...
Read more >Console Port, Telnet, and SSH Handling [Cisco 4000 Series ...
Command or Action, Purpose. Step 1. enable. Example: Router> enable. Enables privileged EXEC mode. Enter your password if prompted. Step 2.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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