Network Automation Netmiko Python

The following blog explains network automation with the use of various Python libraries such as netmiko, paramiko, and many more.

Table of Content

Introuduction to netmiko
Netmiko Installation
Use case : Netmiko to display show ip interface brief
Use case : Netmiko to ssh into Cisco router and display uptime
Use case : Netmiko script to display serial number, system image and configuration register value
Use case : Netmiko script to display arp entries on Cisco router
Use case : Display interfaces of a Cisco router and ask user to select the interface to display its running configuration
Use case : Saving running configuration of a router in a text file
Use case : Netmiko script to change hostname of a router
Use case : Netmiko script to change interface description of a router

Netmiko introduction

Netmiko is a Python library developed by Kirk Byers. It makes it easy to interact with and manage network devices. It is based on the Paramiko library, which is the SSH implementation.

Installation of netmiko

The Netmiko library can be installed by using pip install netmiko. The following screen shot displays content when the library is being installed.

We can check the version of netkimo installed on a system by using the command pip list.

The netmiko version currently being used is 4.1.0.

Use case : Netmiko to access the Cisco router and display show ip interface brief

First of all, import the ConnectHandler class from the netmiko library.

from netmiko import ConnectHandler

The next step is to create an object that handles connection to a device. This object contains information such as host or IP address, username, password, and device type.

net_connect = ConnectHandler(device_type="cisco_xe",host="10.10.20.48",username="developer",password="C1sco12345",)

To send command to a Cisco router, use the send_command() method. The command result will be saved in an output variable.

output = net_connect.send_command("show ip int brief")

Print the result that is saved in an output variable.

print(output)

Output

Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.10.20.48     YES NVRAM  up                    up
GigabitEthernet2       unassigned      YES NVRAM  administratively down down
GigabitEthernet3       unassigned      YES NVRAM  administratively down down

Use case : Netmiko to display uptime of a router

In this second example, we will use netmiko to connect to a Cisco router and display the uptime of a router.

from netmiko import ConnectHandler
conn = ConnectHandler(device_type="cisco_xe",host="10.10.20.48",username="developer",password="C1sco12345",)
out = conn.send_command('show version | include uptime')
print(out)

Output

csr1000v-1 uptime is 42 minutes

Use case : Netmiko to display serial number, system image and configuration register value

In the below example, we have used netmiko to find the serial number, system image, and configuration register value of a router.

>>> from netmiko import ConnectHandler
>>> conn = ConnectHandler(device_type="cisco_xe",host="10.10.20.48",username="developer",password="C1sco12345",)
>>> sn = conn.send_command('show version | include Processor board ID')
>>> config_register = conn.send_command('show version | include Configuration register')
>>> system_image = conn.send_command('show version | include System image file')
>>> print(sn)
Processor board ID 9JW7CMUH5V9
>>> print(config_register)
Configuration register is 0x2102
>>> print(system_image)
System image file is "bootflash:packages.conf"

Use case : Netmiko to display arp entries on Cisco router

The below code displays arp entries from a router.

>>> from netmiko import ConnectHandler
>>> conn = ConnectHandler(device_type="cisco_xe",host="10.10.20.48",username="developer",password="C1sco12345",)
>>> arp = conn.send_command('show arp')
>>> print(arp)
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  10.10.20.20            67   0050.56bf.5eed  ARPA   GigabitEthernet1
Internet  10.10.20.48             -   0050.56bf.5163  ARPA   GigabitEthernet1
Internet  10.10.20.254           64   0050.56bf.e7f9  ARPA   GigabitEthernet1

Use case: Display interfaces of a Cisco router and ask user to select the interface to display its running configuration

from netmiko import ConnectHandler
conn = ConnectHandler(device_type="cisco_xe",host="10.10.20.48",username="developer",password="C1sco12345",)
interfaces = conn.send_command('show ip int brief')
print(interfaces)
print('Enter the interface to display its running configuration')
interface = input('Enter the interface : ')
command = 'show runn interface ' + interface
interface_run = conn.send_command(command)
print(interface_run)

Output

Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.10.20.48     YES NVRAM  up                    up      
GigabitEthernet2       unassigned      YES NVRAM  administratively down down    
GigabitEthernet3       unassigned      YES NVRAM  administratively down down    
Enter the interface to display its running configuration
Enter the interface : GigabitEthernet1
Building configuration...

Current configuration : 171 bytes
!
interface GigabitEthernet1
 description MANAGEMENT INTERFACE - DON'T TOUCH ME
 ip address 10.10.20.48 255.255.255.0
 negotiation auto
 no mop enabled
 no mop sysid
end

Use case : Saving running configuration of a router in a text file

In this program, we will save running configuration of a router into a text file. We will create a file named running_config.txt to save the running configuration of a router. This file needs to be opened in write mode.

First of all, import the ConnectHandler class from the netmiko library.

from netmiko import ConnectHandler

Next, we will create a ConnectHandler object to handle the connection to a device.

conn = ConnectHandler(device_type="cisco_xe",host="sandbox-iosxr-1.cisco.com",username="admin",password="C1sco12345",)

Before we start sending commands to a router, we have to open a file in write mode. This file will be saved in the current working directory. To find the current working directory use os.getcwd() method.

os.getcwd()

Creating a file named running_config.txt in the current working directory

f = open("C:\\Users\\Rohan.Jadhav\\running_config.txt" , "w")

Now send the command “show run” and save the result in a variable.

out = conn.send_command('show run')

Save the result of a variable in a file.

f.write(out)

Below is the content of a file after the program is executed.

Use case : Netmiko to change hostname of a router

In our previous examples, we used netmiko to pull configuration details from a router. Here, we will use it to change the configuration parameters of a router. The below example uses netmiko to change the hostname of a router hosted in a sandbox.

The first step is to import ConnecHandler form Netmiko library

from netmiko import ConnectHandler

Next step involves using the IP address, username and password to login into a device. 

net_connect = ConnectHandler(device_type = 'cisco_ios', host='131.226.217.143', username='developer',password ='C1sco12345')

Once the connection is created with the device, we will use send_command function to get current hostname of a device. 

output = net_connect.send_command("show running | include hostname")A 

A variable command is created, which is assigned with the actual command to change the hostname. The function send_config_set is used to send the command to a router to change the hostname.

command = ['hostname New_Hostname']
net_connect.send_config_set(command)

Finally, print the new hostname

Output

Current hostname of a device: csr1000v-1
Changing the hostname.....
New hostname of a device: New_Hostname

Complete Code

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type = 'cisco_ios', host='131.226.217.143', username='developer',password ='C1sco12345')
output = net_connect.send_command("show running | include hostname")
hostname = list(output.split(' '))
print('Current hostname of a device:',hostname[1])
print('Changing the hostname.....')
command = ['hostname New_Hostname']
net_connect.send_config_set(command)
output = net_connect.send_command("show running | include hostname")
hostname = list(output.split(' '))
print('New hostname of a device:',hostname[1])

Use case : Netmiko script to change description on an interface

Here, we will use the Python netmiko script to change the description of an interface. Initially, we will check if any description has been configured on the interface. The script will display the current value of the description, and later on, we will change it. Interface Gi2 has a description of “Network Interface”, which will change to “Local LAN Interface”

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type = 'cisco_ios', host='131.226.217.143', username='developer',password ='C1sco12345')
output = net_connect.send_command("show interface description")
print(output)
command = ['interface Gi2','description Local LAN Interface']
print('Changing description on the interface Gi2')
net_connect.send_config_set(command)
output = net_connect.send_command("show interface description")
print(output)

Output

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