napalm-automation / napalm

Network Automation and Programmability Abstraction Layer with Multivendor support
Apache License 2.0
2.25k stars 554 forks source link

get_config for IOS and NX-OS (maybe IOS-XR also) #1010

Open ktbyers opened 5 years ago

ktbyers commented 5 years ago

Filter header information out from the running, startup configs such that get_config and load_replace_candidate are complimentary operations.

i.e. you can do get_config on running and then go and immediately load it using replace and expect to get a null diff file back.

mirceaulinic commented 5 years ago

I can confirm that would be needed for IOS-XR as well.

TheRealBecks commented 5 years ago

I connected to one of our devices and I started some investigations:

device#more flash:config.text
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!
version 15.2
service nagle
no service pad

device#sho start
Using 13766 out of 524288 bytes
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!
version 15.2
service nagle
no service pad

device#sho run
Building configuration...

Current configuration : 13766 bytes
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!
version 15.2
service nagle
no service pad

In all three cases the header is not equal. I tested some regex on the website https://regex101.com/ with the following edited test string:

Building configuration...
Current configuration : 13766 bytes
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!
version 15.2
service nagle
! test
!
no service pad

And I got the following regex pattern:

^[\w\W][^!]{0,}^!$\R^![\w\W][^!\r\n]{0,}[\r\n]{1}^![\w\W][^!\r\n]{0,}[\r\n]{1}^!$\R(?=^[\w])

Is it that you are seeking for?

Edit: After discussing with a colleague I corrected some tokens:

^.[^!]*^!$\R^!.[^!\r\n]*[\r\n]{1}^!.[^!\r\n]*[\r\n]{1}^!$\R(?=^[\w])

...and a short version that matches the same string:

^(.[^!])*(^!.*$\R){4}
ktbyers commented 5 years ago

Very complex reg-ex are almost impossible to maintain...so I was thinking something more like:

def config_filter_cisco_ios(cfg):
    """Filter unneeded items that change from the config."""

    patterns = [
        "^Building configuration.*$",
        "^Current configuration.*$",
     ]

    for pattern in patterns:
        cfg = re.sub(pattern, "", cfg, flags=re.M)
    return cfg.strip()

Note, this is not solving the problem of things that change for Git checkin/backup. That is a slightly different problem than this.

To solve that there would be different things that need stripped.

TheRealBecks commented 4 years ago

Ok, one step back to this:

device#sho start
Using 13766 out of 524288 bytes
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!

and:

device#sho run
Building configuration...

Current configuration : 13766 bytes
!
! Last configuration change at 15:27:36 MET-DST Wed Sep 25 2019 by test
! NVRAM config last updated at 15:27:42 MET-DST Wed Sep 25 2019 by test
!

If the configurations shall be comparable the 'using ...' line in 'show startup-configuration' command needs to be deleted. When executing 'show running-configuration' the blank line in line 2 needs to be deleted, but the blank line at the end of the file needs to be preserved (or ignored when comparing two files).

DanSheps commented 1 month ago

Hello everyone,

I noticed that this seems to be solved by https://github.com/napalm-automation/napalm/pull/1190 but introduces some perhaps underiserable behaviour by also removing the running/startup datetime comments. IMO those should not be removed as they don't impact the running of the configuration (or perhaps can be alternatively removed by a argument).

Willing to put in a PR to fix that but would it be better to open it as a separate bug first?

Additionally, IMO, it would be nice to be able to pass a "filter" list that would filter out specific lines beyond those already specified.