netutils icon indicating copy to clipboard operation
netutils copied to clipboard

Use Regex for section_config()

Open racsoce opened this issue 4 years ago • 4 comments

Environment

  • netutils version: 0.2.2

When using section_config() function passing the sections is using "startswith" and will be convenient to use Regex.

Proposed Functionality

In file: netutils/netutils/config/compliance.py

import re

line 421 if not match and line.config_line.startswith(line_start): change to: if not match and re.search(line_start, line.config_line):

Use Case

looking for specific access list names like:

ip access-list auto_golden_AB

having

ip access-list auto_golden_ABX
ip access-list auto_golden_AB
ip access-list auto_golden_ABC
ip access-list auto_golden_AB1
ip access-list auto_golden_AB2

we can extract the exact match defining the end of line ($):

section_config({'name':'ACL', 'ordered': True, "section":['ip access-list auto_golden_AB$']}, intended, 'cisco_nxos')

or the ones that have a number after (\d+):

section_config({'name':'ACL', 'ordered': True, "section":['ip access-list auto_golden_AB\d+']}, intended, 'cisco_nxos')

Thanks,

racsoce avatar Oct 06 '21 22:10 racsoce

Would supporting full line match suffice? There are implications and I would have to think them through for allowing just a regex.

itdependsnetworks avatar Oct 07 '21 02:10 itdependsnetworks

I was thinking in a more flexible way to get desired lines from the "config_parsed" object using the existing method section_config().

We can force start of line using: if not match and re.search( "^" + line_start, line.config_line):

But it also works supporting full line match, wondering how we tell the method to do full match? maybe in the features adding "full_match": True,:

features = [{ "name": "BGP",
              "ordered": True,
              "full_match": True,
              "section": [
                 "router bgp 100"
              ]
}]

Thanks,

racsoce avatar Oct 07 '21 15:10 racsoce

Yes, I was thinking something like if not match and re.search( "^" + line_start, line.config_line): with some checks. The problem is if we left it full regex, there is likely to be other issues for how the rest of the system works.

For full_match: True, there is other issues as well, but I think there is something in the earlier suggestion that should work for this use case.

itdependsnetworks avatar Oct 07 '21 15:10 itdependsnetworks

I have another use-case for the regex feature:

want to match: interface port-channel100

but not

interface port-channel1001

This is currently only possible when using custom function. I think this would be a good improvement!

andreasbourges avatar Dec 20 '22 15:12 andreasbourges