Napalm and Cisco IOS

Categories Automation, Cisco, Devnet, Python
Napalm

Copy and pasta into a text file name it CHANGES.cfg

vlan 999
name BLACKHOLE
default int ra fa 1/0/10 - 20
int ra fa 1/0/10 - 20
sw mo acc
sw acc vlan 999
vlan 120
name TEST

Copy and pasta into a text file name it whatever you like with a .py python extension.

import json
from netmiko import ConnectHandle
from napalm import get_network_driver

driver = get_network_driver('ios')
c3750 = driver('10.0.10.24','lab','lab')
c3750.open()

print ('Accessing..')
c3750.load_merge_candidate(filename='CHANGES.cfg')

print ('Comparing Config Changes to Current Running Configuration.')
diffs = c3750.compare_config()
if len(diffs) > 0:
print(diffs)
c3750.commit_config()
else:
print('No changes needed.')
c3750.discard_config()

print ('Saving Config..')
c3750.commit_config()
print ('Config Saved..')
c3750.close()
print ('Connection Closed..')

Netmiko: Configure multiple devices with multiple commands (Cisco IOS)

Categories Automation, Cisco, Devnet
Automation

Create a script and save it as config.py
Create a text file with your IOS script inside – This example is named c3750_lab_design

Save the file in the same folder path you save the script.

Example of Cisco IOS Script:

vtp mode transparent
spanning-tree mode rapid-pvst
udld enable
errdisable recovery cause all
port-channel load-balance src-dst-ip
ip name-server 10.0.10.3
no ip http server
no ip http secure-server

snmp-server community python1 RO
snmp-server community python2 RW

ntp server 0.us.pool.ntp.org

clock timezone CST -6
clock summer-time CST recurring
service timestamps debug datetime msec localtime
service timestamps log datetime msec localtime

vlan 10
name Data
vlan 11
name Voice
vlan 12
name Test

interface vlan 1
description In-band Management

ip default-gateway 10.0.10.1
ip dhcp snooping vlan 100,101

no ip dhcp snooping information option
ip dhcp snooping
ip arp inspection vlan 10
spanning-tree portfast bpduguard default

interface range Fa 1/0/1 - 20
switchport
switchport access vlan 10
switchport voice vlan 11
switchport host
switchport port-security maximum 5
switchport port-security
switchport port-security aging time 5
switchport port-security aging type inactivity
switchport port-security violation restrict
ip arp inspection limit rate 100
ip dhcp snooping limit rate 100
ip verify source

interface range Fa 1/0/21 - 48
switch tr en do
switchport mode trunk
end
wr

Configure multiple Cisco IOS devices with Netmiko

Categories Automation, Cisco, Devnet
Automation

Related Posts:

Copy pasta script and save as config.py:

from netmiko import ConnectHandler

c3750_s1 = {
'device_type': 'cisco_ios',
'ip': '10.0.10.24',
'username': 'lab',
'password': 'lab'
}

c3750_s2 = {
'device_type': 'cisco_ios',
'ip': '10.0.10.25',
'username': 'lab',
'password': 'lab'
}

c3750_s3 = {
'device_type': 'cisco_ios',
'ip': '10.0.10.26',
'username': 'lab',
'password': 'lab'
}

all_devices = [c3750_s1, c3750_s2, c3750_s3]

for devices in all_devices:
net_connect = ConnectHandler(**devices)
for n in range (2,21):
print ("Creating VLAN " + str(n))
config_commands = ['vlan ' + str(n), 'name Python_VLAN' + str(n)]
output = net_connect.send_config_set(config_commands)
print (output)

Execute the script and then check the devices for configuration changes.

Netmiko SSH Script to Cisco 3750 Switch

Categories Automation, Cisco, Devnet
Automation

Open your favorite editor and copy pasta:

from netmiko import ConnectHandler
c3750 = {
'device_type': 'cisco_ios',
'ip': '10.0.10.24',
'username': 'lab',
'password': 'lab'
}

net_connect = ConnectHandler(**c3750)
output = net_connect.send_command('show ip int brief')
print (output)

config_commands = ['int loop 0', 'ip address 1.1.1.1 255.255.255.0']
output = net_connect.send_config_set(config_commands)
print (output)

for n in range (2,21):
print ("Creating VLAN " + str(n))
config_commands = ['vlan ' + str(n), 'name Python_VLAN' + str(n)]
output = net_connect.send_config_set(config_commands)
print (output)

Save the file as config.py and execute the script:
python config.py

If we check on our switch we can see that our script has created new vlans.