Network Automation : How to handle connection error in Napalm

Network Automation NAPALM (Python networking scripts examples). Network automation with python tutorial

We have used the NAPALM script to connect network devices to obtain configuration details such as interfaces IP address or mac address table.

In these cases, we already had knowledge of the IP address, username, and password and, using those details, we were able to connect to a device and retrieve configuration details.

Now, let’s say you are trying to connect to a device and every time you run the script you get the below output.

The above output says that we are not able to connect to a device. There might be some issues, such as a wrong IP address or an incorrect username or password.

There is a simple script which displays the message ” Connection Failed” instead of displaying all exceptions in the output. This is just a basic script to handle connection errors.

from napalm import get_network_driver
import json
driver = get_network_driver('ios')
host = ['131.226.217.143','131.226.0.143']
for h in host:
device = driver(h,'developer','C1sco12345')
try:
device.open()
print('Connection Successful to host %s' %h)
except:
print('Connection to host %s failed' %h)

In the above code, we have created a list with two IP addresses. The first is a valid IP address, while the second is the incorrect IP address. When the program is executed, it displays the message “Connection Successful to Host” when it attempts to connect to 131.226.217.143. “Connection to host is failed” is displayed when it tries to connect to a second IP address.

Output

Connection Successful to host 131.226.217.143
Connection to host 131.226.0.143 failed

Links to other posts for network automation with NAPALM

Introduction to NAPALM

Use get_facts() method to retrieve device information

NAPALM Context Manager to connect to a device

Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s