ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.55k stars 1.3k forks source link

How to suppress the "Common Causes" on Netmiko exceptions #2271

Closed jrtrussell81 closed 3 years ago

jrtrussell81 commented 3 years ago

Is there guidance on how to suppress the "Common Causes" output for connecthandler exceptions? I am already printing a short explanation...

So I do not want to see:

Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: cisco_wlc_ssh 1.1.1.1:22

ktbyers commented 3 years ago

Should be able to catch the exception and then raise your own exception with your own message.

Kirk

jrtrussell81 commented 3 years ago

I am catching the exception, but after it catches it gives the common causes...

Connecting to TEST @ WLC IP 1.1.1.1 (this is mine)


Failed to TEST @ WLC IP 1.1.1.1 TCP connection to device failed. (this is my catch message)

Common causes of this problem are:
1. Incorrect hostname or IP address.
2. Wrong TCP port.
3. Intermediate firewall blocking access.

Device settings: cisco_wlc_ssh 1.1.1.1:22
jrtrussell81 commented 3 years ago

nm_exceptions = (nm.ssh_exception.NetMikoTimeoutException, nm.ssh_exception.NetMikoAuthenticationException)

try: connect_wlc = nm.ConnectHandler(ip=current_wlc_ip, banner_timeout=10, device_type='cisco_wlc_ssh', username=username, password=password) out1 = '' for cmd in commands: out1 += connect_wlc.send_command(cmd) print(out1) except nm_exceptions as e: print('Failed to', current_wlc, '@ WLC IP ' + current_wlc_ip, e) connect_wlc.disconnect()

jrtrussell81 commented 3 years ago

Should be able to catch the exception and then raise your own exception with your own message.

Kirk

I am catching the exception, but after it catches it gives the common causes...

Connecting to TEST @ WLC IP 1.1.1.1 (this is mine)

Failed to TEST @ WLC IP 1.1.1.1 TCP connection to device failed. (this is my catch message)

Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: cisco_wlc_ssh 1.1.1.1:22

EndlessDynamics commented 3 years ago

Should be able to catch the exception and then raise your own exception with your own message.

Kirk

I am catching the exception, but after it catches it gives the common causes...

Connecting to TEST @ WLC IP 1.1.1.1 (this is mine)

Failed to TEST @ WLC IP 1.1.1.1 TCP connection to device failed. (this is my catch message)

Common causes of this problem are:

  1. Incorrect hostname or IP address.

  2. Wrong TCP port.

  3. Intermediate firewall blocking access.

Device settings: cisco_wlc_ssh 1.1.1.1:22

Hello. You are catching the exception as 'e', then printing your custom statement however, you include the exception 'e' at the very end of your print statement which is why it is still printing it. Remove the variable e and it should work the way you want.

jrtrussell81 commented 3 years ago

Should be able to catch the exception and then raise your own exception with your own message.

Kirk

I am catching the exception, but after it catches it gives the common causes... Connecting to TEST @ WLC IP 1.1.1.1 (this is mine) Failed to TEST @ WLC IP 1.1.1.1 TCP connection to device failed. (this is my catch message) Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: cisco_wlc_ssh 1.1.1.1:22

Hello. You are catching the exception as 'e', then printing your custom statement however, you include the exception 'e' at the very end of your print statement which is why it is still printing it. Remove the variable e and it should work the way you want.

You are definitely correct...I was just hoping to see the actual exception whether it be timeout or authentication...unfortunately the timeout exception carries with it the "common causes" paragraph which I don't really care for...all I was wanting to see is the "TCP connection to device failed" part...o well. Thanks!

ktbyers commented 3 years ago

@jrtrussell81 You have the string in the variable e (str(e)). You can parse that string and extract what you want from it.

You can also have multiple separate except statements if you want to catch the different types of exceptions.

jrtrussell81 commented 3 years ago

@jrtrussell81 You have the string in the variable e (str(e)). You can parse that string and extract what you want from it.

You can also have multiple separate except statements if you want to catch the different types of exceptions.

I am not to sure how to do that exactly...do you have example code?