Skip to content

Regex is too generic for BGP session matching #976

@naveci

Description

@naveci

Hi all,

I was trying to create some tests for an upcoming change to make an easy comparison of a before and after situation. However, I noticed during development that the regex pattern here:

# default 0 65000 0 0 Established NSR Ready
p1_2 = re.compile(r'^(?P<vrf>\S+) +(?P<spk>\d+) +'
r'(?P<as_number>\d+) +(?P<in_q>\d+) +(?P<out_q>\d+) +'
r'(?P<nbr_state>\w+) +(?P<nsr_state>[\w\s]+)$')

is also matching against other outputs in my files, simply because the regex is too generic.

Here's an example output from a test device:

RP/0/RP1/CPU0:test-pe#show bgp sessions
Tue Dec  2 10:09:59.045 CET

Neighbor        VRF                   Spk    AS   InQ  OutQ  NBRState     NSRState
111.111.111.11  default                 0 65000     0     0  Established  NSR Ready
111.111.111.12  default                 0 65000     0     0  Idle  NSR Ready
10.1.189.21     default                 0 65000.21     0     0  Established  NSR Ready
10.1.189.23     default                 0 65000.23     0     0  Established  NSR Ready
10.1.189.31     default                 0 65000.31     0     0  Established  NSR Ready
10.1.189.33     default                 0 65000.33     0     0  Established  NSR Ready
RP/0/RP1/CPU0:test-pe#
RP/0/RP1/CPU0:test-pe#show hsrp summary
Tue Dec  2 10:17:29.643 CET
                IPv4                  IPv6
State   Sessions Slaves Total  Sessions Slaves Total
-----   -------- ------ -----  -------- ------ -----
ALL           11      0    11        11      0    11

ACTIVE         0      0     0         0      0     0
STANDBY        0      0     0         0      0     0
SPEAK          0      0     0         0      0     0
LISTEN         0      0     0         0      0     0
LEARN          0      0     0         0      0     0
INIT          11      0    11        11      0    11

11   HSRP IPv4 interfaces    (0    up, 11   down)
11   HSRP IPv6 interfaces    (0    up, 11   down)
11   Virtual IPv4 addresses  (0    active, 11   inactive)
11   Virtual IPv6 addresses  (11   active, 0    inactive)
1    Tracked Objects    (1    up, 0    down)
0    BFD sessions       (0    up, 0    down, 0    inactive)
RP/0/RP1/CPU0:test-pe#

It should never use the output from HSRP. Why is there no "boundary" of the commands? There's a clear indication on the CLI when a command is used. I can build this myself now as a workaround, but shouldn't things such as # and > be a clear demarcation?
Alternatively, I believe there should always be an IPv4 or IPv6 address be in front (although I'm not 100% sure), why not check for that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions