Python – Script13: Configure Multiple Devices multiple Interfaces config file via SSH (Netmiko)

Step1: Configure file with all interface details
	nano iosv_l2_access
Vlan 2
int range g2/0 – 3
switchport mode access
switchport access vlan 2
int range g0/0 – 1
switchport trunk encapsulation dot1q
switchport mode trunk
switchport nonegotiate
switchport trunk allowed vlan1,2
spanning-tree link-type point-to-point
nano iosv_l2_core
Vlan 2
int range g0/1 – 3
switchport trunk encapsulation dot1q
switchport mode trunk
switchport nonegotiate
switchport trunk allowed vlan1,2
spanning-tree link-type point-to-point
int range g1/0 – 1
switchport trunk encapsulation dot1q
switchport mode trunk
switchport nonegotiate
switchport trunk allowed vlan1,2
spanning-tree link-type point-to-point
Step2: Configure file with for python script
	nano int_config_netmiko.py
from netmiko import ConnectHandler
 
iosv_l2_s1 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.10.21’,
    ‘username’: ‘rachit’,
    ‘password’: ‘cisco’,
    ‘secret’: ‘cisco’
}
 
iosv_l2_s2 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.10.22’,
    ‘username’: ‘rachit’,
    ‘password’: ‘cisco’,
    ‘secret’: ‘cisco’
}
 
iosv_l2_s3 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.10.23’,
    ‘username’: ‘rachit’,
    ‘password’: ‘cisco’,
    ‘secret’: ‘cisco’
}
 
iosv_l2_s4 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.10.24’,
    ‘username’: ‘rachit’,
    ‘password’: ‘cisco’,
    ‘secret’: ‘cisco’
}
 
iosv_l2_s5 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.10.25’,
    ‘username’: ‘rachit’,
    ‘password’: ‘cisco’,
    ‘secret’: ‘cisco’
}
 
with open(‘iosv_l2_access’) as f:
    lines = f.read().splitlines()
print lines
 
all_devices = [iosv_l2_s5, iosv_l2_s4, iosv_l2_s3]
 
for devices in all_devices:
    net_connect = ConnectHandler(**devices)
    net_connect.enable()
    output = net_connect.send_config_set(lines)
    print output
 
with open(‘iosv_l2_core’) as f:
    lines = f.read().splitlines()
print lines
 
all_devices = [iosv_l2_s1, iosv_l2_s2]
 
for devices in all_devices:
    net_connect = ConnectHandler(**devices)
    net_connect.enable()
    output = net_connect.send_config_set(lines)
    print output

Network Architect | CCIEx3 #29824 JNCIE #2197 VCIX-NV

Leave a Comment