n3s0 || journal

Automate Adding A VLAN To Multiple Cisco Switches

Posted on 3 mins

Netadmin Code

Table of Contents

Summary


Wrote another script. This one is useful should there be any batch configuration tasks needed on switches. Definately useful when you need to add a VLAN for multiple devices.

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

n3s0/scripts/cisco_add_vlan_batch.py - Dead Link

This script has a list of multiple devices and their connection details. These are lab devices.

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.
  8. Moves to the next switch in the list named “devices” until it reaches the end of the list.
#!/bin/python

from netmiko import ConnectHandler

til_asw_01 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.11',
    'username': 'admin',
    'password': ''
}
til_asw_02 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.12',
    'username': 'admin',
    'password': ''
}
til_asw_03 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.13',
    'username': 'admin',
    'password': ''
}
til_asw_04 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.14',
    'username': 'admin',
    'password': ''
}
til_asw_05 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.15',
    'username': 'admin',
    'password': ''
}
til_asw_06 = {
    'device_type': 'cisco_ios',
    'ip': '192.168.10.16',
    'username': 'admin',
    'password': ''
}

devices = [til_asw_01, til_asw_02, til_asw_03, til_asw_04,
           til_asw_05, til_asw_06]

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

for device in devices:
    net_connect = ConnectHandler(**device)
    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...')
    print ('')
    end_script_vlans = net_connect.send_command('show vlan')
    print (end_script_vlans)

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

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

This script performed the same operations was the one written previously. Only now it can reach more devices on the network. This script can be used for a lot more then just configuring VLANs with a little modification. The whole point to this is to get familiar with the library so it can be used for bigger and better things down the road.

At some point I’ll write a script that will backup the configuration for multiple switches and diagnostic data. With the This can be useful for initially dropping into an environment for work and you need to cover your bases. My rule of thumb when entering any environment to perform network work is to make sure I backup network configuration before it starts. This isn’t just for CYA though. It’s also good to review the current configuration before making changes so you know what is going on.