A regular expression to find equal length paths (or unequal)

Answered Question
Jun 25th, 2007
User Badges:

Hi all.


Has anyone got a clue as to whether it is possible to construct a regular expression to show only those prefixes who have an equal (or unequal) number of AS hops in their paths. These prefixes are coming from 2 different ISPs so may differ in terms of actual AS path, but that doesn't matter, it's the number of AS traversed I care about.


Thanks in advance, kind regards.

Correct Answer by Pavel Bykov about 10 years 1 month ago

RegExp always evaluates prefixes one by one.

You can match prefixes of the specific length. E.g.:

^[0-9]+_[0-9]+_[0-9]+$ Would match all prefixes with AS PATH length of 3.

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 4.6 (5 ratings)
Loading.
Correct Answer
Pavel Bykov Mon, 06/25/2007 - 02:40
User Badges:
  • Silver, 250 points or more

RegExp always evaluates prefixes one by one.

You can match prefixes of the specific length. E.g.:

^[0-9]+_[0-9]+_[0-9]+$ Would match all prefixes with AS PATH length of 3.

dogfugitive Mon, 06/25/2007 - 03:28
User Badges:

Hi. Once I replaced the "+" with "*" this does the job perfectly. Thanks very much for your assistance.

milan.kulik Mon, 06/25/2007 - 05:03
User Badges:
  • Red, 2250 points or more

Hi do you also know how to filter only the best paths displayed by sh ip bgp regexp?


Something like

sh ip bgp regexp .... | include >

(which doesn't work unfortunately)?


Thanks,

Milan

Pavel Bykov Thu, 06/28/2007 - 07:05
User Badges:
  • Silver, 250 points or more

Well, that's the problem. whole line is fed into REGEXP for evaluation, so it does not matter if there is a pipe "|" or anything. It will just look for that exression in AS PATH...


I tried it with unix double pipe, like "more `show ip bgp regexp ...`, but it doesn't work.


If it is possible, than it will be with some unix trick.

milan.kulik Thu, 06/28/2007 - 23:22
User Badges:
  • Red, 2250 points or more

Yes, and in some cases piping would even lose some info: if the best path is not the first one for particular prefix, the subnet info would be lost.


So the only way I found was to capture the show ip bgp regexp ... output, put it to an Excel sheet and make some data sorting manually :-(


BR,

Milan

Pavel Bykov Fri, 06/29/2007 - 07:07
User Badges:
  • Silver, 250 points or more

Ok, i have found out how to do it right in the command line :)

Still interested?

milan.kulik Mon, 07/02/2007 - 02:09
User Badges:
  • Red, 2250 points or more

Hi, yes, still interested.


hritter described a solution using quote-regexp.

Do you know something else?


Thanks,

Milan


Harold Ritter Fri, 06/29/2007 - 11:00
User Badges:
  • Cisco Employee,

Milan,


You can use show ip bgp quoted-regexp instead of regexp to allow the use of the pipe statement on the IOS cli.


route-server>sh ip bgp quote-regexp "23456" | incl >

Status codes: s suppressed, d damped, h history, * valid, > best, i - internal

*>i 66.59.190.224 0 110 0 6461 12654 23456 i

*>i145.125.0.0/20 66.59.190.228 0 100 0 2914 3549 1103 1125

23456 i

*>i192.26.93.0 66.59.190.228 0 100 0 2914 4697 23456 i

*>i 66.59.190.227 0 110 0 13237 5539 23456 i

route-server>


Hope this helps,

milan.kulik Mon, 07/02/2007 - 01:57
User Badges:
  • Red, 2250 points or more

Hi Harold,


yes, it works, thanks!


One additional question:

Using this pipe, I'm losing the subnet info sometimes:


route-server.ams2>sh ip bgp quote-regexp "_80$"

BGP table version is 77002010, local router ID is 67.17.81.187

Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,

r RIB-failure, S Stale

Origin codes: i - IGP, e - EGP, ? - incomplete


Network Next Hop Metric LocPrf Weight Path

* i3.0.0.0 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

*>i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i205.173.92.0 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

*>i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i208.234.185.0 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

*>i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

* i 67.17.64.73 600 199 0 701 703 80 i

route-server.ams2>

route-server.ams2>

route-server.ams2>

route-server.ams2>sh ip bgp quote-regexp "_80$" | include >

Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,

*>i 67.17.64.73 600 199 0 701 703 80 i

*>i 67.17.64.73 600 199 0 701 703 80 i

*>i 67.17.64.73 600 199 0 701 703 80 i

route-server.ams2>


Do you now some way how to keep the subnets in the output?


Thanks a lot,

Milan



Pavel Bykov Mon, 07/02/2007 - 02:18
User Badges:
  • Silver, 250 points or more

My way is using UNIX trick.

Basically, you execute normal regexp command, without pipe, and hit enter.

Afterwards (after first screen of output)

type +>

and press enter. Afterwards, you can use spacebar.


If you adjust your terminal screen to only sever lines long, then use the +> trick, then adjust the terminal screen again, you would get only the best routes. Otherwise you will get one screen of mixed routes, and rest of the screens best routes

Pavel Bykov Mon, 07/02/2007 - 02:54
User Badges:
  • Silver, 250 points or more

P.S.: typing "/word" in an output takes you to the next occurence of "word"

milan.kulik Mon, 07/02/2007 - 03:57
User Badges:
  • Red, 2250 points or more

Hi,


I know /word from vi editor, but where is the + word coming from?

(I'm a Unix beginner.)


BR,

Milan



Pavel Bykov Mon, 07/02/2007 - 04:48
User Badges:
  • Silver, 250 points or more

In linux MORE command the +/ option specifies a string that will be searched for before each file is displayed.

In MORE the + doesn't actually work when already displaying a file, but it does in ios

Harold Ritter Mon, 07/02/2007 - 05:39
User Badges:
  • Cisco Employee,

Milan,


This is an additional challenge since the > is not necessarely beside the first path, which is the only path that gets the prefix information in the output.


The best I could come up with is the following:


sh ip bgp quote "regexp" | incl i[0-9]+|>


In the case the first path is not the best path (doesn't have the ">"), you need to print both the first path and the best path resulting into two lines in the output. If the first path and the best path happen to be the same then only one line is generated for this prefix. Here's an example:


route-server>sh ip bgp quote "23456" | incl i[0-9]+|>

Status codes: s suppressed, d damped, h history, * valid, > best, i - internal

* i84.205.88.0/24 66.59.190.229 0 110 0 6461 12654 23456 i

*>i 66.59.190.224 0 110 0 6461 12654 23456 i

*>i145.125.0.0/20 66.59.190.228 0 100 0 2914 3549 1103 1125

23456 i

*>i192.26.93.0 66.59.190.228 0 100 0 2914 4697 23456 i

* i193.31.7.0 66.59.190.229 0 110 0 1273 5539 23456 i

*>i 66.59.190.227 0 110 0 13237 5539 23456 i

* i202.255.47.0 66.59.190.229 0 110 0 2516 7667 23456 i

*>i 66.59.190.224 0 110 0 2516 7667 23456 i

route-server>



Hope this helps,

Harold Ritter Mon, 07/02/2007 - 05:54
User Badges:
  • Cisco Employee,

My previous regexp was only taking into account ibgp learnt prefixes ("i").


This one will cope with both eBGP and iBGP learnt prefixes:


sh ip bgp quote-regexp "23456" | incl \* [i, ][0-9]+|>


Hope this helps,

milan.kulik Mon, 07/02/2007 - 06:05
User Badges:
  • Red, 2250 points or more

Hi Harold,


THANKS!

That's exactly what I need!


I see I have to learn a lot how to work with the regular expessions...


BR,

Milan

Actions

This Discussion