I’ve been looking for ways to make my job easier in regards to configuring Cisco devices. I think I figured out a way and it works great!

I will be putting the link for this script found in one of my respositories below.

This is a Python script I wrote that will add a VLAN to a switch with the name net-mgmt. This is kind of similar to Ansible. But, it takes less time to put everything together in my opinion. You do a little configuration, run it, and off it goes.

This can be customized in whichever way anyone would like to. I would recommend changing “til_csw_01” to the hostname for a switch you’d like to configure. Then change the IP Address and credentials as well.

A little more about what the script does.

  1. Connects to the Cisco device via SSH using the configuration.
  2. Get a list of the current VLANs before the work starts.
  3. Obtains output of the switches current running configuration.
  4. Runs the commands found in the vlan_commands list. In this case it will create VLAN 10 and give it the name net-mgmt.
  5. Gets a list after the work is completed so there’s confirmation.
  6. Saves the running-config to the startup-config.
  7. Disconnects from the device.
#!/bin/python

from netmiko import ConnectHandler

til_csw_01 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.100',
    'username': 'admin',
    'password': '',
}

vlan_commands = [
    "vlan 10",
    "name net-mgmt",
]

net_connect = ConnectHandler(**til_csw_01)

print ('Obtaining VLAN configuration before...')
print ('')
start_script_vlans = net_connect.send_command('show vlan')
print (start_script_vlans)

print ('Obtain the configuration for the switch...')
print ('')
start_script_config = net_connect.send_command('show running-config')
print (start_script_config)

print ('Configuring the VLAN')
print ('')
vlan_config_output = net_connect.send_config_set(vlan_commands)
print (vlan_config_output)

print ('Obtaining VLAN configuration after...')
end_script_vlans = net_connect.send_command('show vlan')
print (end_script_vlans)

print ('Saving configuration...')
net_connect.save_config()

print ('Disconnecting from device...')
net_connect.disconnect()

There are plenty of other use cases for this. I’ll be posting one for doing this with a batch of switches here for example. I was just providing the simplest example I could. Another idea after posting the batch configuration update is configuration backup of all of the gear in batches. Coming soon.