Example Usage: Build configuration diffs

Getting diffs is a common need when something happens and you want to know “what changed” between two configs. This example will demonstrate how to find diffs between two configurations.

Note

Diffs of both files or multi-line strings are supported; however, a single-line string is always understood as a file name.

Baseline Configurations

bucksnort_before.conf

This tutorial will run all the queries this example base configuration, the “before” version is shown below.

 1! Filename: /tftpboot/bucksnort_before.conf
 2!
 3hostname bucksnort
 4!
 5interface Ethernet0/0
 6 ip address 1.1.2.1 255.255.255.0
 7 no cdp enable
 8!
 9interface Serial1/0
10 encapsulation ppp
11 ip address 1.1.1.1 255.255.255.252
12!
13interface Serial1/1
14 encapsulation ppp
15 ip address 1.1.1.5 255.255.255.252
16!

bucksnort_after.conf

This tutorial will run diff against this example after configuration, which has MPLS enabled on ‘Serial1/0’.

 1! Filename: /tftpboot/bucksnort_after.conf
 2!
 3hostname bucksnort
 4!
 5interface Ethernet0/0
 6 ip address 1.1.2.1 255.255.255.0
 7 no cdp enable
 8!
 9interface Serial1/0
10 encapsulation ppp
11 ip address 1.1.1.1 255.255.255.252
12 mpls ip
13!
14interface Serial1/1
15 encapsulation ppp
16 ip address 1.1.1.5 255.255.255.252
17!

Diff Script

The script below will build read the configurations from disk and check to see whether there are diffs.

>>> from ciscoconfparse2.ciscoconfparse2 import Diff
>>> # Parse the original configuration
>>> old_config = '/tftpboot/bucksnort_before.conf'
>>> new_config = '/tftpboot/bucksnort_after.conf'
>>> diff = Diff(old_config=old_config, new_config=new_config)
>>> diff.get_diff()
['interface Serial1/0', ' mpls ip']
>>>
>>> for line in diff.get_diff():
...     print(line)
...
!
interface Serial1/0
 mpls ip
!
>>>

Rollback Script

The script below will build read the configurations from disk and build rollback diff configs.

>>> from ciscoconfparse2.ciscoconfparse2 import Diff
>>> # Parse the original configuration
>>> old_config = '/tftpboot/bucksnort_before.conf'
>>> new_config = '/tftpboot/bucksnort_after.conf'
>>> diff = Diff(old_config=old_config, new_config=new_config)
>>> diff.get_rollback()
['interface Serial1/0', ' mpls ip']
>>>
>>> for line in diff.get_rollback():
...     print(line)
...
!
interface Serial1/0
 no mpls ip
!
>>>