cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting - basic tasks (does a VLAN exist?, etc.)

Scripting - basic tasks (does a VLAN exist?, etc.)

EtherNation_Use
Contributor II
Create Date: Apr 12 2013 4:50PM

Hi,

I'm writing some scripts to automate some aspects of configuring switches - for example to create a VLAN, along with the associated STPD with Cisco Rapid-PVST+, etc. That part isn't specifically a problem, but I'm trying to put some error checking into the script to detect things like a VLAN with the same name already existing (and not running the script, leaving things in a half-configured state, etc.).

This all means I have a number of basic checks I want to do in the scripts. I've managed to do these, but I have a feeling either I'm missing something easy, or things are not quite as straightforward as I feel they should be. I'm posting them here so someone might be able to improve them or, if they are as good as they can get, someone might find them useful!

I'm running XOS 12.6.something.

The basic checks are:

    find out is a VLAN name "X" already exists assuming it does, what is its 802.1Q tag? find out if 802.1Q VLAN tag is already in use
Below I'll put the code I'm using and what the complications are...

set var CLI.OUT 0
show vlan | include ^\S+\s+[0-9]+\s
set var vlan_list $TCL(split ${CLI.OUT} " ")
set var vlan_idx $TCL(lsearch -regexp $vlan_list [join [concat "(?i)^" $vlan_name "\\s+\[0-9\]+\\s"] ""])

if ($vlan_idx == -1) then
set var CLI.OUT "Error: VLAN $vlan_name does not exist!"
show var CLI.OUT

The problems I had were:

    For some reason, you can't use "show vlan NAME" in a script I can't just do "show vlan | include ^$vlan_name\s" to find a VLAN -- the "\s" on the end stops the match - I've tried putting { } around the variable name but no success - you can use \s with a literal string (as I am doing). You can't use "| include REGEXP" and match a case-insensitive regexp, so I need to do this separately. I can't just join a string together in embedded TCL -- trying "lsearch -regexp $vlan_list ^$vlan_name\s+[0-9]+\s" doesn't see to work (I've tried lots of escaping). Hence I have to muck about with the "[join [concat ...] ""]" to get a string. I can't seem to put "if ($TCL(...) == -1) then" -- $TCL() doesn't seem to work here, so I have to do it separately and compare the result. I can't just print a literal string with "show" - I reuse CLI.OUT to avoid using a variable I have to delete later.
To get the VLAN tag for a named VLAN, I do the above and then:

set var vlan_entry $TCL(lindex $vlan_list $vlan_idx)
set var vlan_tag $TCL(lindex $vlan_entry 1)

... I then can't nest the last two expressions with "set var vlan_tag $TCL(lindex [lindex $vlan_list $vlan_idx] 1)" -- that complains $vlan_idx doesn't exist.

The bit to check if an 802.1Q tag is in use is:

set var CLI.OUT 0
show vlan | include ^\S+\s+[0-9]+\s
set var vlan_idx $TCL(lsearch -regexp [split ${CLI.OUT} " "] [join [concat "^\\S+\\s+" $vlan_tag "\\s"] ""])

if ($vlan_idx != -1) then
set var CLI.OUT "Error: 802.1Q Tag $vlan_tag is already assigned!"
show var CLI.OUT

This has a similar problem to finding out if a VLAN name has been used.

If anyone has any comments, please let me know!

- Bob (from Mince)
0 REPLIES 0
GTM-P2G8KFN