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.

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 ciscoconfparse.ciscoconfparse import Diff
>>> # Parse the original configuration
>>> before_lines = open('/tftpboot/bucksnort_before.conf').read().splitlines()
>>> after_lines = open('/tftpboot/bucksnort_after.conf').read().splitlines()
>>> diff = Diff(hostname='bucksnort', old_config=before_lines, new_config=after_lines)
>>> diff.diff()
['interface Serial1/0', ' mpls ip']
>>>
>>> for line in diff.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 ciscoconfparse.ciscoconfparse import Diff
>>> # Parse the original configuration
>>> before_lines = open('/tftpboot/bucksnort_before.conf').read().splitlines()
>>> after_lines = open('/tftpboot/bucksnort_after.conf').read().splitlines()
>>> diff = Diff(hostname='bucksnort', old_config=before_lines, new_config=after_lines)
>>> diff.diff()
['interface Serial1/0', ' mpls ip']
>>>
>>> for line in diff.rollback():
...     print(line)
...
!
interface Serial1/0
 no mpls ip
!
>>>