genieparser icon indicating copy to clipboard operation
genieparser copied to clipboard

Regex is too generic for BGP session matching

Open naveci opened this issue 2 months ago • 4 comments

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: https://github.com/CiscoTestAutomation/genieparser/blob/b1aa02bd44c92207b078259cbe911a2db891927b/src/genie/libs/parser/iosxr/show_bgp.py#L5616-L5619

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?

naveci avatar Dec 02 '25 10:12 naveci

One note/example, I set one router on purpose to Idle and changed the HSRP sessions from 12 to 11. This results in the following genie diff output:

BGP Session Differences:
 instance:
  default:
   vrf:
    ALL:
     neighbors:
      111.111.111.12:
-      in_q: 12
+      in_q: 11
-      nsr_state: 12
+      nsr_state: 11
-      out_q: 12
+      out_q: 11
-      spk: 12
+      spk: 11
    INIT:
     neighbors:
      111.111.111.12:
-      in_q: 12
+      in_q: 11
-      nsr_state: 12
+      nsr_state: 11
-      out_q: 12
+      out_q: 11
-      spk: 12
+      spk: 11
    default:
     neighbors:
      111.111.111.12:
-      nbr_state: Established
+      nbr_state: Idle

If i leave the HSRP intact (to 12), then i only see the last few lines, which is what I would expect. However, the regex issue causes incorrect output for BGP.

naveci avatar Dec 02 '25 10:12 naveci

It should not be parsing "show hsrp summary" output with the "show bgp sessions" parser. Perhaps you are not using the tool correctly. Can you share your code you are using to run this?

aknopper avatar Dec 04 '25 14:12 aknopper

Hi Alex,

I'm aware it should not be parsing that command. What I'm trying to achieve is to have a script that will validate things before and after a change. As our environments are strictly separated, it's not easy to run a Python script in another environment, even more so when external libraries are required. So my method of working has been to run a bunch of commands, copy the output into a text file outside of that environment and use it for a quick check.

I'm aware that this is not neat netdevops, but for verifications during a change window, this would already be massively helpful.

Given that context, yes there will be commands in such a file that would get parsed by the incorrect parsers. So either the parser has to be more specific (lookbehind or lookaheads) or I need to change something in the handling of the file and try to separate the commands from each other and then use the correct parsers.

naveci avatar Dec 04 '25 14:12 naveci

How are you passing this text file into genie? I thought that it had to connect to a device and run the commands on it.

aknopper avatar Dec 04 '25 14:12 aknopper