Network Automation

Netmiko Multiple Devices

Previous post already talking about network automation using netmiko, but on that post only show how to automate configuration on the single device. In Addition, this post will be covering how to automate configuration using netmiko multiple devices in sequence. This post will using the same network topology as previous, please refer to picture as below:

On this automation showing how to display and do the configuration. The sample script will be running on IOS and IOS-XE devices, R6, R7, CSR3, and CSR2. This automation script will add new interface loopback 10 with description netmiko-sequence, and display the all interface IP Address on each routers.

The first thing to do is declare all the devices, using dictionary. Then, create a list that consist all of the devices, and make the iteration based on the previous list. To get the detail information about duration of the script executed, this post using datetime module.

netmiko-sequence.py

from netmiko import ConnectHandler
from datetime import datetime

CSR2 = {
    'device_type': 'cisco_xe',
    'ip': '172.16.1.2',
    'username': 'cisco',
    'password': 'cisco',
    'secret': 'cisco'
}

CSR3 = {
    'device_type': 'cisco_xe',
    'ip': '172.16.1.3',
    'username': 'cisco',
    'password': 'cisco',
    'secret': 'cisco'
}
R6 = {
    'device_type': 'cisco_ios',
    'ip': '172.16.1.6',
    'username': 'cisco',
    'password': 'cisco',
    'secret': 'cisco'
}

R7 = {
    'device_type': 'cisco_ios',
    'ip': '172.16.1.7',
    'username': 'cisco',
    'password': 'cisco',
    'secret': 'cisco'
}


all_devices =  [CSR2, CSR3, R6, R7]
start_time = datetime.now()
a = 0
for device in all_devices:
    net_connect = ConnectHandler (**device)
    net_connect.enable()
    print ("Before Configuration " + all_devices[a]["ip"])
    output = net_connect.send_command('show ip int brief') 
    print ("##############################################")
    print (output)
    config_commands = ['int loop 10', 'ip address 1.1.1.1 255.255.255.255']
    config_commands1 = ['int loop 10', 'description netmiko-sequence']
    output = net_connect.send_config_set(config_commands)
    output = net_connect.send_config_set(config_commands1)
    print ("##############################################")
    print ("Creating New Loopbak 10 " + (all_devices[a]["ip"]))
    print ("##############################################")
    print ("After Configuration " + all_devices[a]["ip"])
    output2 = net_connect.send_command('show int description')
    output3 = net_connect.send_command('show ip int brief') 
    print ("##############################################")
    print ("Show Interface Description")
    print (output2)
    print ("Show IP Interface Brief")
    print (output3)
    print ("##############################################")
    a = a + 1 

end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))

Result of Netmiko Multiple Devices

root@Server:/home/admin/devnet# python3 netmiko-sequence.py
Before Configuration 172.16.1.2
##############################################
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.172.4.2      YES manual up                    up      
GigabitEthernet2       10.172.5.2      YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       unassigned      YES unset  administratively down down    
Loopback0              172.16.1.2      YES manual up                    up      
##############################################
Creating New Loopbak 10 172.16.1.2
##############################################
After Configuration 172.16.1.2
##############################################
Show Interface Description
Interface                      Status         Protocol Description
Gi1                            up             up       to_CSR3
Gi2                            up             up       to XRV5
Gi3                            admin down     down     
Gi4                            admin down     down     
Gi5                            admin down     down     
Lo0                            up             up       
Lo10                           up             up       netmiko-sequence
Show IP Interface Brief
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.172.4.2      YES manual up                    up      
GigabitEthernet2       10.172.5.2      YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       unassigned      YES unset  administratively down down    
Loopback0              172.16.1.2      YES manual up                    up      
Loopback10             1.1.1.1         YES manual up                    up      
##############################################
Before Configuration 172.16.1.3
##############################################
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.172.4.1      YES manual up                    up      
GigabitEthernet2       10.172.3.1      YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       unassigned      YES unset  administratively down down    
Loopback0              172.16.1.3      YES manual up                    up      
##############################################
Creating New Loopbak 10 172.16.1.3
##############################################
After Configuration 172.16.1.3
##############################################
Show Interface Description
Interface                      Status         Protocol Description
Gi1                            up             up       to_CSR2
Gi2                            up             up       to_R7
Gi3                            admin down     down     
Gi4                            admin down     down     
Gi5                            admin down     down     
Lo0                            up             up       
Lo10                           up             up       netmiko-sequence
Show IP Interface Brief
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.172.4.1      YES manual up                    up      
GigabitEthernet2       10.172.3.1      YES manual up                    up      
GigabitEthernet3       unassigned      YES unset  administratively down down    
GigabitEthernet4       unassigned      YES unset  administratively down down    
GigabitEthernet5       unassigned      YES unset  administratively down down    
Loopback0              172.16.1.3      YES manual up                    up      
Loopback10             1.1.1.1         YES manual up                    up      
##############################################
Before Configuration 172.16.1.6
##############################################
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                10.172.1.2      YES manual up                    up      
Ethernet0/1                10.172.2.1      YES manual up                    up      
Ethernet0/2                unassigned      YES TFTP   up                    up      
Ethernet0/3                unassigned      YES TFTP   up                    up      
Loopback0                  172.16.1.6      YES manual up                    up      
##############################################
Creating New Loopbak 10 172.16.1.6
##############################################
After Configuration 172.16.1.6
##############################################
Show Interface Description
Interface                      Status         Protocol Description
Et0/0                          up             up       to_XRV4
Et0/1                          up             up       to_R7
Et0/2                          up             up       
Et0/3                          up             up       
Lo0                            up             up       
Lo10                           up             up       netmiko-sequence
Show IP Interface Brief
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                10.172.1.2      YES manual up                    up      
Ethernet0/1                10.172.2.1      YES manual up                    up      
Ethernet0/2                unassigned      YES TFTP   up                    up      
Ethernet0/3                unassigned      YES TFTP   up                    up      
Loopback0                  172.16.1.6      YES manual up                    up      
Loopback10                 1.1.1.1         YES manual up                    up      
##############################################
Before Configuration 172.16.1.7
##############################################
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                unassigned      YES TFTP   up                    up      
Ethernet0/1                10.172.2.2      YES manual up                    up      
Ethernet0/2                10.172.3.2      YES manual up                    up      
Ethernet0/3                unassigned      YES TFTP   up                    up      
Loopback0                  172.16.1.7      YES manual up                    up      
##############################################
Creating New Loopbak 10 172.16.1.7
##############################################
After Configuration 172.16.1.7
##############################################
Show Interface Description
Interface                      Status         Protocol Description
Et0/0                          up             up       
Et0/1                          up             up       to_R6
Et0/2                          up             up       to_CSR3
Et0/3                          up             up       
Lo0                            up             up       
Lo10                           up             up       netmiko-sequence
Show IP Interface Brief
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                unassigned      YES TFTP   up                    up      
Ethernet0/1                10.172.2.2      YES manual up                    up      
Ethernet0/2                10.172.3.2      YES manual up                    up      
Ethernet0/3                unassigned      YES TFTP   up                    up      
Loopback0                  172.16.1.7      YES manual up                    up      
Loopback10                 1.1.1.1         YES manual up                    up      
##############################################
Duration: 0:00:35.879748
root@Server:/home/admin/devnet# 

After executing netmiko script, there is new Loopback 10 interfaces along with the interface description. Using datetime module, the time of execution using netmiko multiple devices in sequences can be calculated and shown as 0:00:35.879. The next post will be covering configuration of multiple devices in simultaneously.

Leave a Reply

Your email address will not be published. Required fields are marked *