Created
May 16, 2024 09:39
-
-
Save Pr1meSuspec7/240a2201fb392ad636e0b763915fbe3b to your computer and use it in GitHub Desktop.
NETMIKO | Manage TimeoutException and AuthenticationException
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html | |
from pprint import pprint | |
import yaml | |
import netmiko | |
def send_show_command(device, commands): | |
result = {} | |
try: | |
with netmiko.ConnectHandler(**device) as ssh: | |
ssh.enable() | |
for command in commands: | |
output = ssh.send_command(command) | |
result[command] = output | |
return result | |
except netmiko.NetmikoTimeoutException: | |
output = ( | |
net_device["host"] + "_" + net_device["ip"] | |
+ ": ERROR [Device unreachable]" | |
) | |
print(output) | |
continue | |
except netmiko.NetmikoAuthenticationException: | |
output = ( | |
net_device["host"] + "_" + net_device["ip"] | |
+ ": ERROR [Authentication failed]" | |
) | |
print(output) | |
continue | |
device = { | |
"device_type": "cisco_ios", | |
"host": "192.168.10.10", | |
"username": "cisco", | |
"password": "cisco", | |
} | |
result = send_show_command(device, ["sh ip int br"]) | |
pprint(result, width=120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment