Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. And see here for current known issues.

New Member

Assistance with Regular Expression and Tcl

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
Cisco Employee

This worked for me. Switch_1

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

 

2 REPLIES
Cisco Employee

This worked for me. Switch_1

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

 

New Member

Daniel.    Thanks.  That does

Daniel.

 

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

 

Thanks again.

Chris

351
Views
0
Helpful
2
Replies