cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1212
Views
19
Helpful
8
Replies

TCL scripting

voiper_99
Level 1
Level 1

Hi, could someone please explain the following script to me? And also, how could I edit it so it just showed dialer interfaces? I have read about regular expressions but I find them quite confusing.

Thanks in advance.

proc get_ints {} {

#puts "BEFORE\n"

set check ""

set int_out [ exec "show interfaces\n" ]

puts $int_out

puts ""

set mylist [regexp -all -nocase -line -inline {(^[a-z]*[0-9]/*[0-9]*/*[0-9]*)} $int_out]

#puts "MYLIST $mylist\n"

foreach int $mylist {

#puts "INT $int\n"

if {![string equal $check $int]} {

if {[info exists ints_out]} {

append ints_out ", " $int

} else {

set ints_out $int

}

set check $int

}

}

#puts "AFTER\n"

return $ints_out

}

puts ""

puts [eval get_ints]

8 Replies 8

Joe Clarke
Cisco Employee
Cisco Employee

proc get_ints {match } {

set output [exec "show interfaces"]

set intlist [regexp -all -line -inline "^$match\[\\d\\./:]+" $output]

return [join $intlist ","]

}

puts ""

puts [get_ints "Dialer"]

FYI, this script calls a TCL proc called "get_ints" with a single argument. The argument specifies the interface pattern you wish to match ("Dialer" in this case). The get_ints proc executes a "show interfaces" command, and goes through the output searching for lines that begin with the desired match pattern followed by one or more digit, period, forward slash, or colon. The resulting list of matching interfaces is then joined together with commas, and returned to the caller which then prints the list out to the controlling terminal.

The end result will be a list of Dialer interfaces on this device.

I tried the above script but received the following error:

no value given for parameter "match" to "get_ints"

I tried adding a "set match dialer" line but this did not work.

You need to call the get_ints proc with a parameter indicating the interface pattern you wish to match. If you pasted the script exactly how I wrote it, you should get a list of Dialer interfaces on the device. If you wanted a list of FastEthernet interfaces:

puts [get_ints "FastEthernet"]

ahh OK then.

I just have one more question (for now anyway) :P

I have successfully created an if statement that only accepts a number between 1 and 8, if I try to enter the number 9 it will repeat the question again, however, if I enter a letter I get the following message:

"can't use non-numeric string as operand of "+""

Is it possible to further restrict my if statement so that if a letter is entered the question will repeat itself until a valid number is entered? I know I can use the "or" || but is it possible to "or" a-z instead of one letter at a time?

set done 0

while { ! $done } {

puts -nonewline "Enter value (1 - 8) : "

flush stdout

gets stdin response

if { [string is digit -strict $response] && $response > 0 && $response < 9 } {

set done 1

}

}

I have successfully completed my script and it works perfectly, however, I have two small issues.

The first is that the router will crash if I press crtl+shift+6, and seeing as though I am not the only person that will be using the router that I have put the script on it would be good if I could stop this from occurring. Is there a way to prevent this from happening?

The second issue I am having is that if more than one person uses the script at a time on the one router the script will crash.

As stated above I am very new to TCL scripting so please bare with me if these are simple questions. Thank you.

Please provide a show stack after the crash, the script, and exactly what you mean when you say the script crashes.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: