1

I need to verify what ip address is announce the default route:

set command [ exec "show ip route" ]

the ouput:

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       + - replicated route, % - next hop override

Gateway of last resort is 10.17.1.252 to network 0.0.0.0

B*    0.0.0.0/0 [200/0] via 10.17.1.252, 01:16:22
      10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C        10.17.1.0/24 is directly connected, FastEthernet1/0
L        10.17.1.253/32 is directly connected, FastEthernet1/0
      172.22.0.0/32 is subnetted, 1 subnets
C        172.22.12.250 is directly connected, Loopback1
      172.26.0.0/16 is variably subnetted, 3 subnets, 2 masks
B        172.26.69.64/30 [200/0] via 10.17.1.252, 01:16:33
C        172.26.70.64/30 is directly connected, FastEthernet0/0
L        172.26.70.66/32 is directly connected, FastEthernet0/0

I need to find this: "0.0.0.0/0 [200/0] via 10.17.1.252", but this address: 10.17.1.252 can change, how I can put a variable in a regex ?

set routes [ regexp {(0.0.0.0\/0 \[200\/0\] via $bgp_neighbor)} $command match default_route ]
CFRR
  • 45
  • 1
  • 7
  • So, `$bgp_neighbor` contains the IP address you need to use as part of the regex, right? And what value do you expect for `default_route`? – Wiktor Stribiżew Feb 08 '18 at 23:06
  • yes, $bgp_neighbor contain the IP address. actually I need verify only if is true, if the default route with that ip exists – CFRR Feb 08 '18 at 23:11

2 Answers2

3

A regular expression isn't the best choice for this.

string first [format {0.0.0.0/0 [200/0] via %s} $bgp_neighbor] $command 

When searching for a zero variance pattern, string first is the closest match. Building the pattern with format minimizes the amount of escaping you need to do.

In this case, there isn't much escaping needed anyway:

string first "0.0.0.0/0 \[200/0] via $bgp_neighbor" $command

Note that string first returns the position of the string if found, or -1 if not found.

Documentation: string

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
  • your solution of yours is also interesting and managed to incorporate in the remaining code I have, thanks for the tip. – CFRR Feb 09 '18 at 08:58
2

You need to build a dynamic regex keeping in mind that unescaped . match any char in regex, and that is why you need to escape the dynamic part before adding it to the regex.

Here is a way to solve the issue ($a is the variable holding the text):

proc reEscape {str} {
    regsub -all {\W} $str {\\&}
}
set bgp_neighbor {10.17.1.252}
set bgp_neighbor_escaped [reEscape $bgp_neighbor]
set reg {\y0\.0\.0\.0/0 \[200/0] via }
append reg $bgp_neighbor_escaped
append reg {\y}

if {[regexp $reg $a]} {
  puts yes
} else {
  puts no
}

See the Tcl demo

Here, $bgp_neighbor holds the IP address. The $bgp_neighbor_escaped is the same string with all non-word chars escaped (you will actually have . replaced with \.). The \y are word boundaries, in order to avoid matching 152.1.0.23 in 2222152.1.0.234444.

Note you may replace all literal spaces with \s+ to match any 1+ whitespace chars.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Wiktor Stribiżew, just perfect! Thanks for the help! – CFRR Feb 09 '18 at 01:12
  • R1(tcl)#proc reEscape {str} { +> regsub -all {\W} $str {\\&} +>} R1(tcl)#set bgp_neighbor {172.26.69.65} 172.26.69.65 R1(tcl)#set bgp_neighbor_escaped [reEscape $bgp_neighbor] wrong # args: should be "regsub ?switches? exp string subSpec varName" R1(tcl)#set reg {\y0\.0\.0\.0/0 \[20/0] via } \y0\.0\.0\.0/0 \[20/0] via R1(tcl)#append reg $bgp_neighbor_escaped can't read "bgp_neighbor_escaped": no such variable R1(tcl)#append reg {\y} \y0\.0\.0\.0/0 \[20/0] via \y R1(tcl)# is not working in different version of IOS or if i put inside of proc – CFRR Feb 09 '18 at 02:52
  • @CFRR Have a look at Peter's solution, it might be what you need for the current scenario. – Wiktor Stribiżew Feb 09 '18 at 07:19