gaggi
gaggi

Reputation: 11

Need help on a simple TCL script regarding regexp

I'm trying to write a tcl script basically to do following. Based on the syslog below,

LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/0/23, changed state to down

When that log is seen on the router, the script needs to push out a shell command which includes the interface index number, in this case it is 23. I can use regex to scrape the interface in the syslog by doing this below.

set interface ""
if {[regexp {.* (GigabitEthernet1/0/[0-9]*)} $syslog_msg match interface]}
if [catch {cli_exec $cli1(fd) "show port_diag unit 1 port 23"} result] {
error $result $errorInfo
}
}

But how can I use only the interface index number (which is 23) in the command above? Do I need to extract [0-9]* from the regexp and store it as a vairable or somethig like that?

Upvotes: 1

Views: 77

Answers (1)

tshiono
tshiono

Reputation: 22012

Please just enclose the expression [0-9]* with parentheses and append a variable name, say num, to be assigned to the second capture group. Here is a snipped code to demonstrate:

if {[regexp {.* (GigabitEthernet1/0/([0-9]*))} $syslog_msg match interface num]} {
    puts $num
}

Output:

23

If the result looks okay, modify the command within the curly braces to perform your task as:

if {[regexp {.* (GigabitEthernet1/0/([0-9]*))} $syslog_msg match interface num]} {  
    if [catch {cli_exec $cli1(fd) "show port_diag unit 1 port $num"} result] {
        error $result $errorInfo
    }
}

Upvotes: 1

Related Questions