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
driver = get_network_driver('ios')
host = ['18.104.22.168','22.214.171.124']
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 126.96.36.199. “Connection to host is failed” is displayed when it tries to connect to a second IP address.
Connection Successful to host 188.8.131.52 Connection to host 184.108.40.206 failed
Links to other posts for network automation with NAPALM
Use get_facts() method to retrieve device information