Automating dynamic port VLAN changes on Cisco IOS
Posted: Fri Apr 17, 2020 2:59 pm
A small script that will automate access VLAN change on ports on Cisco IOS (including IOS-XE).
You set a source VLAN and a target VLAN.
This will find all access ports in the source VLAN, and change these to the desired target vlan.
Make sure to set "Require Enable mode" so Unimus switches the device to privileged exec before running the commands.
This uses the Cisco IOS tclsh to dynamically loop over all interfaces and make changes.
Unimus is used for script delivery and result validation network-wide.
More info on tclsh in IOS: https://www.cisco.com/c/en/us/td/docs/i ... t-tcl.html
You set a source VLAN and a target VLAN.
This will find all access ports in the source VLAN, and change these to the desired target vlan.
Make sure to set "Require Enable mode" so Unimus switches the device to privileged exec before running the commands.
This uses the Cisco IOS tclsh to dynamically loop over all interfaces and make changes.
Unimus is used for script delivery and result validation network-wide.
More info on tclsh in IOS: https://www.cisco.com/c/en/us/td/docs/i ... t-tcl.html
Code: Select all
tclsh
set source_vlan 100
set target_vlan 200
set ifaces [split [exec "show interface status"] "\n"]
foreach if_line $ifaces {
if [regexp {^([^ ]+) {2}.+ {2}.+ +([0-9]+) .+} $if_line match ifo vlan] {
if {$vlan == $source_vlan} {
ios_config "interface $ifo" "switchport access vlan $target_vlan"
}
}
}