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 = ['','']
for h in host:
device = driver(h,'developer','C1sco12345')
print('Connection Successful to host %s' %h)
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 “Connection to host is failed” is displayed when it tries to connect to a second IP address.


Connection Successful to host
Connection to host 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


Leave a Comment

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

You are commenting using your 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