question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling exception when SSH is not enabled on Cisco devices.

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
devopsnetworkscommented, Jan 23, 2021

@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#

1reaction
ktbyerscommented, Jul 6, 2016

@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?

$ python
Python 2.7.10 (default, Dec  8 2015, 18:25:05) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> paramiko.__version__
'2.0.1'

Kirk

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found