cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1969
Views
5
Helpful
2
Replies

Assistance with Regular Expression and Tcl

Chris Clifton
Level 1
Level 1

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

 

1 Accepted Solution

Accepted Solutions

Dan Frey
Cisco Employee
Cisco Employee

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)#

 

View solution in original post

2 Replies 2

Dan Frey
Cisco Employee
Cisco Employee

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)#

 

Daniel.

 

   Thanks.  That does indeed work and helps educate me on the proper structure using regular expression with tcl.

 

Thanks again.

Chris

Review Cisco Networking for a $25 gift card