Eleck
Eleck

Reputation: 307

TCL regex issue - Finding a router parent interface

I'm doing some Expect / TCL scripting to grab important information from routers.

One of my objectives is to grab the name of the WAN interface of a router using the IP I logged into the router as a reference.

Example Router IP: 66.66.66.66 - This is contained within variable $rtrname.

Using expect, I evaluate the below data ($expect_out(buffer)) with the following expression, and output it to the value $wan...

Expression:

regexp (.*?)(?=(?:\..*?)?\s{2,}$rtrname) $expect_out(buffer) wan

$expect_out(buffer):

Router1#sh ip int brief
Interface                  IP-Address      OK? Method Status                Protocol
Embedded-Service-Engine0/0 unassigned      YES NVRAM  administratively down down    
GigabitEthernet0/0         unassigned      YES NVRAM  up                    up      
GigabitEthernet0/0.10      55.55.55.55  YES NVRAM  up                    up      
GigabitEthernet0/0.20      66.66.66.66  YES NVRAM  up                    up      
GigabitEthernet0/1         77.77.77.77   YES NVRAM  up                    up      
GigabitEthernet0/2         192.168.1.1   YES NVRAM  up                    up   

With $rtrname == 66.66.66.66 this returns $wan as GigabitEthernet0/0 correctly, which is what I want.

Unfortunately, it's not working properly with an output such as this:

Router2#sh ip int brief
Interface               IP Address          Status      Protocol
eth 0/1                192.168.1.1            UP          UP         
eth 0/2                77.77.77.77       UP          UP         
fr 1.501               55.55.55.55       UP          UP         
fr 1.502               66.66.66.66       UP          UP     

This results in $wan coming back as nothing, when I'm expecting 'fr 1'.

I've worked with a few different regex tools and the expression seems to pass. It's just when I drop it in code that it doesn't. What am I missing?

PS - I'm a network engineer, not a professional coder by trade, so be easy on me! :)

Upvotes: 1

Views: 409

Answers (1)

Jerry
Jerry

Reputation: 71578

Are you sure you're getting GigabitEthernet0/0? Because the regex is wrong and should be:

regexp -line -- ^(.*?)(?=(?:\\..*?)?\\s{2,}$rtrname) $expect_out(buffer) wan
                            ^       ^

The -line flag is to make ^ matches the beginning of a line instead of the whole string.

Since you're not wrapping your expression within braces, you have to double escape the backslashes.

And the regex works for me on the other output as well.

Upvotes: 1

Related Questions