Hiding traceback from exceptions

Hi all,

When using a Nornir subtask which leverages the Netmiko plugin, is it possible to hide/suppress the Netmiko traceback message which is caused by either a timeout or authentication error?

As an example:

def get_version(task):
    try:
        result = task.run(task=netmiko_send_command, command_string="sh ver")
        print(f"{task.host.name} : Success!")
    except NornirSubTaskError as e:
        if isinstance(e.result.exception, NetMikoTimeoutException):
            print(f"{task.host.name} : Error! Timeout")
        elif isinstance(e.result.exception, NetMikoAuthenticationException):
            print(f"{task.host.name} : Error! Failed authentication")
        else:
            raise e

If this causes an exception(e.g. authentication failure) I still see the following:

$ python nornir_exceptions2.py
Host 'NX-SW1': task 'netmiko_send_command' failed with traceback:
Traceback (most recent call last):
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\netmiko\base_connection.py", line 884, in establish_connection
    self.remote_conn_pre.connect(**ssh_connect_params)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\paramiko\client.py", line 446, in connect
    passphrase,
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\paramiko\client.py", line 764, in _auth
    raise saved_exception
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\paramiko\client.py", line 751, in _auth
    self._transport.auth_password(username, password)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\paramiko\transport.py", line 1509, in auth_password
    return self.auth_handler.wait_for_response(my_event)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\paramiko\auth_handler.py", line 250, in wait_for_response
    raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\nornir\core\task.py", line 85, in start
    r = self.task(self, **self.params)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\nornir\plugins\tasks\networking\netmiko_send_command.py", line 26,
in netmiko_send_command
    net_connect = task.host.get_connection("netmiko", task.nornir.config)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\nornir\core\inventory.py", line 302, in get_connection
    extras=conn.extras,
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\nornir\core\inventory.py", line 352, in open_connection
    configuration=configuration,
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\nornir\plugins\connections\netmiko.py", line 58, in open
    self.connection = ConnectHandler(**parameters)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\netmiko\ssh_dispatcher.py", line 246, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\netmiko\base_connection.py", line 317, in __init__
    self._open()
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\netmiko\base_connection.py", line 322, in _open
    self.establish_connection()
  File "C:\Users\johnd\code\sandbox\venv\lib\site-packages\netmiko\base_connection.py", line 897, in establish_connection
    raise NetMikoAuthenticationException(msg)
netmiko.ssh_exception.NetMikoAuthenticationException: Authentication failure: unable to connect cisco_nxos 172.16.10.163:22
Authentication failed.

NX-SW1 : Error! Failed authentication

Thanks,
John

Hi John

How did you go on this, I presume you have set logging to False? The easiest solution would be to set Nornir logging to True.

But if you don’t want to do that, I think there is a current issue with logging set to False.

A workaround until fixed that I have used is from https://stackoverflow.com/questions/27647077/fully-disable-python-logging

import logging

logging.getLogger(“nornir”).addHandler(logging.NullHandler())
logging.getLogger(“nornir”).propagate = False

Anthony

1 Like

Hi Anthony,

Thank you for the reply. I had no idea it was as simple as enabling logging. Both solutions work, but I think I’ll stick with enabling logging as this will help with troubleshooting.

Thanks,
John

1 Like