Network Automation : Asking user to provide device details to login

Network Automation NAPALM (Python script example for network engineers)

In this network automation example, we ask user to provide details to login into the device.

First, import get_network_driver form NAPALM

from napalm import get_network_driver

In this example, we are trying to connect to a Cisco IOS router and will be using ‘ios’ as the network driver.

driver = get_network_driver('ios')

Now, ask the user to provide device IP address, username and password.

ip = input('Enter an IP address to connect: ')
username = input('Enter a username: ')
passw = input('Enter a password: ')

Submitting values received from the user to form a connection.

device = driver(ip, username, passw)

Next, try to establish a connection to a router with the details shared by the user. If the connection is successful, we get the message “Connection Successful,” otherwise, “Connection Failed’.

try:
device.open()
print('Connection Successful to %s'%ip)
except:
print('Connection Failed')
print('Please provide proper IP, username and password')

Output (Connection Successful)

Enter an IP address to connect: 131.226.217.143
Enter a username: developer
Enter a password: C1sco12345
Connection Successful

Output (Connection Failed)

Enter an IP address to connect: 1.1.1.1
Enter a username: admin
Enter a password: admin
Connection Failed
Please provide proper IP, username and password

Complete Code

from napalm import get_network_driver
import json
import getpass
driver = get_network_driver('ios')
ip = input('Enter an IP address to connect: ')
username = input('Enter a username: ')
passw = input('Enter a password: ')
device = driver(ip, username, passw)
try:
    device.open()
    print('Connection Successful to %s'%ip)
except:
    print('Connection Failed')
    print('Please provide proper IP, username and password')
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