Python ciscoconfparse - check specific line in a config

64 views Asked by At

I'm trying to get the AAA configuration in my switch to compare the exact configuration, but when i try to gather with the exact same config it doesn't show that as a correct configuration.

Here's the example :

def check_config(config):
    parse = CiscoConfParse('test_config.txt', syntax='ios')
    tcs_obj = parse.find_lines(config)
    
    if parse.find_objects(config):
        print(parse.find_objects(config))
    else:
        print(f'Config {config} not found!!!')


check_config('aaa authentication login default group tacacs+ local')

The result:

Config aaa authentication login default group tacacs+ local not found!!!

But when i tried to remove the 'local', it showed the line.

check_config('aaa authentication login default group tacacs+')

[<IOSCfgLine # 34 'aaa authentication login default group tacacs+ local'>]

1

There are 1 answers

0
Anthony Sylvester On

The reason is because CiscoConfParse objects use regex for their input. So, when you pass aaa authentication login default group tacacs+ local, the + acts as a regex operator.

So, the solution here is to pass a regex string to the function and use the escape character \ on the regex operator +.

Below config works:

from ciscoconfparse import CiscoConfParse


def check_config(config):
    parse = CiscoConfParse('test_config.txt', syntax='ios')

    if parse.find_objects(config):
        print(parse.find_objects(config))
    else:
        print(f'Config {config} not found!!!')


check_config(r'aaa authentication login default group tacacs\+ local')

All that was updated was the string passed to the function check_config. Put an r before the quotes and put an escape \ before the + operator.