cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2899
Views
10
Helpful
4
Replies

VLAN change

MutahirS95
Level 1
Level 1

Hi Everyone,

 

I'm a junior CCNA, and I've been to devnet express. It was amazing but it blew over my head.

 

In my current environment we're changing our phones and we'll be adding the new phones to a new VLAN. What I understood from the Devnet Express course is that I'd be able to change the voice VLAN with a script rather than going into each switch and running the command under the interface range.

 

what I'd like to do is something like:

if the interface has voice VLAN x

   change to voice VLAN y

 

Is this achievable with a python script or anything else I might not know about?

 

Thank you in advance,

Mutahir

1 Accepted Solution

Accepted Solutions

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

This would be trivial to achieve in python. Use the netmiko library to ssh onto each switch and retrieve the running config for each interface in sequence (eg: sh int gi0/1, etc), create a list of these strings, regex each list element for the presence of the voice vlan configuration, record the interface ID if you find a match, then create a string representing the config and send that back to the switch you are working are.

Below is some of the code to get you started:

import re

voice_output = "interface {0}\n  switchport voice vlan {1}"

inte_str = ["interface GigabitEthernet0/1\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 20\n  no shutdown",
            "interface GigabitEthernet0/2\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 20\n  no shutdown",
            "interface GigabitEthernet0/3\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 30\n  no shutdown"]


def find_voice_vlan(interface_str, vlan_id):
    if "switchport voice vlan {0}".format(vlan_id) in interface_str:
        m = re.match("interface\\s(?P<int_id>.*)", interface_str.splitlines()[0])
        if m:
            return m.group("int_id")
    return None


def check_and_change(old_voice_vlan, new_voice_vlan):
    ints_to_change = []
    for int in inte_str:
        target = find_voice_vlan(int, old_voice_vlan)
        if target is not None:
            ints_to_change.append(voice_output.format(target, new_voice_vlan))

    print(ints_to_change)


if __name__ == "__main__":
    check_and_change(20, 21)

This takes the input inte_str, looks for voice vlan 20 and the gives the adjusted configuration in a list which should then be applied back to the switch:

C:\Users\sebastian.rupik\PycharmProjects\csc_tools\venv\Scripts\python.exe C:/Users/sebastian.rupik/.PyCharmCE2018.2/config/scratches/scratch_3.py
['interface GigabitEthernet0/1\n  switchport voice vlan 21', 'interface GigabitEthernet0/2\n  switchport voice vlan 21']

I'll leave it to you to come up with the netmiko config...there are plenty of examples online:

https://github.com/sebrupik/srupik-apic-em-tools/blob/master/frozenPony/src/oneLinerSimple.py

 

;)

 

cheers,

Seb.

 

 

View solution in original post

4 Replies 4

balaji.bandi
Hall of Fame
Hall of Fame

You can do with Python or any other scripting.

 

first get the interface information, process with vlan information, make the script to change vlan and test it.

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

This would be trivial to achieve in python. Use the netmiko library to ssh onto each switch and retrieve the running config for each interface in sequence (eg: sh int gi0/1, etc), create a list of these strings, regex each list element for the presence of the voice vlan configuration, record the interface ID if you find a match, then create a string representing the config and send that back to the switch you are working are.

Below is some of the code to get you started:

import re

voice_output = "interface {0}\n  switchport voice vlan {1}"

inte_str = ["interface GigabitEthernet0/1\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 20\n  no shutdown",
            "interface GigabitEthernet0/2\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 20\n  no shutdown",
            "interface GigabitEthernet0/3\n  switchport mode access\n  switchport access vlan 10\n  switchport voice vlan 30\n  no shutdown"]


def find_voice_vlan(interface_str, vlan_id):
    if "switchport voice vlan {0}".format(vlan_id) in interface_str:
        m = re.match("interface\\s(?P<int_id>.*)", interface_str.splitlines()[0])
        if m:
            return m.group("int_id")
    return None


def check_and_change(old_voice_vlan, new_voice_vlan):
    ints_to_change = []
    for int in inte_str:
        target = find_voice_vlan(int, old_voice_vlan)
        if target is not None:
            ints_to_change.append(voice_output.format(target, new_voice_vlan))

    print(ints_to_change)


if __name__ == "__main__":
    check_and_change(20, 21)

This takes the input inte_str, looks for voice vlan 20 and the gives the adjusted configuration in a list which should then be applied back to the switch:

C:\Users\sebastian.rupik\PycharmProjects\csc_tools\venv\Scripts\python.exe C:/Users/sebastian.rupik/.PyCharmCE2018.2/config/scratches/scratch_3.py
['interface GigabitEthernet0/1\n  switchport voice vlan 21', 'interface GigabitEthernet0/2\n  switchport voice vlan 21']

I'll leave it to you to come up with the netmiko config...there are plenty of examples online:

https://github.com/sebrupik/srupik-apic-em-tools/blob/master/frozenPony/src/oneLinerSimple.py

 

;)

 

cheers,

Seb.

 

 

THANK YOU SO MUCH

No problem.

 

You may also want to look at Ansible. Your playbook would end up being much shorter than a python script, albeit a little less portable.

 

Please mark this post answered if you believe it is :)

 

cheers,

Seb.