07-07-2014 12:59 PM
Assistance with Regular Expression and Tcl
Hello Everyone,
I recently began learning Tcl to develop scripts for automating network switch deployments.
In my script, I want to name the device with a location and the last three octets of the base mac address.
I can get the Base MAC address by :
show version | include Base
Base ethernet MAC Address : 00:00:00:DB:CE:00
And I can get the last three octets of the MAC address using the following regular expression.
([0-9a-f]{2}[:-]){2}([0-9a-f]{2}$)
But I have not been able to figure out how to call the regular expression in the tcl script.
I have checked several resources but have not been able to figure it out. Suggestions?
Ultimately, I want to set the last three octets to a variable (something like below) and then call the variable when I name the switch.
set mac [exec "sh version | i Base"] (include the regular expression)
ios_config "hostname location$mac"
Thanks for any assistance in advance.
Chris
Solved! Go to Solution.
07-08-2014 06:54 AM
This worked for me.
Switch_1(tcl)#set result [exec show ver | inc Base]
Base ethernet MAC Address : 00:1B:D4:F8:B1:80
Switch_1(tcl)#regexp {([0-9A-F:]{8}\r)} $result -> mac
1
Switch_1(tcl)#puts $mac
F8:B1:80
Switch_1(tcl)#ios_config "hostname location$mac"
%Warning! Hostname should contain at least one alphabet or '-' or '_' character
locationF8:B1:80(tcl)#
07-08-2014 06:54 AM
This worked for me.
Switch_1(tcl)#set result [exec show ver | inc Base]
Base ethernet MAC Address : 00:1B:D4:F8:B1:80
Switch_1(tcl)#regexp {([0-9A-F:]{8}\r)} $result -> mac
1
Switch_1(tcl)#puts $mac
F8:B1:80
Switch_1(tcl)#ios_config "hostname location$mac"
%Warning! Hostname should contain at least one alphabet or '-' or '_' character
locationF8:B1:80(tcl)#
07-08-2014 08:57 AM
Daniel.
Thanks. That does indeed work and helps educate me on the proper structure using regular expression with tcl.
Thanks again.
Chris
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide