Akshay J
Akshay J

Reputation: 5468

difference between 2 string holding Cisco device's config

I have 2 variables, config1 and config2 holding the running config of a Cisco device at 2 different points in time.

Sample running config:

version 12.3
no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname retail
!
boot-start-marker
boot-end-marker
!
enable password cisco123
!
username jsomeone password 0 cg6#107X
aaa new-model
!
aaa group server radius rad_eap
    server 10.0.1.1 auth-port 1812 acct-port 1813
!
aaa authentication login eap_methods group rad_eap
aaa session-id common
ip subnet-zero
ip cef
!
vpdn enable
    vpdn-group 1
    request-dialin
    protocol pppoe
!
interface dialer 1
    ip address negotiated
    ppp authentication chap
    dialer pool 1
    dialer-group 1
!
dialer-list 1 protocol ip permit
    ip nat inside source list 1 interface dialer 0 overload
    ip classless (default)
    ip route 10.10.25.2 0.255.255.255 dialer 0
!
ip dhcp excluded-address 10.0.1.1 10.0.1.10
ip dhcp excluded-address 10.0.2.1 10.0.2.10
ip dhcp excluded-address 10.0.3.1 10.0.3.10
!
ip dhcp pool vlan1
   network 10.0.1.0 255.255.255.0
   default-router 10.0.1.1 
!

config1 holds the default config and config2 holds the config after doing a test. Ultimately, config2 should be the same as config1.

What I need is a way to find a "diff" between config2 and config1 (config2-config1). Something that I get from websites like https://text-compare.com/ which shows side-by-side comparison.

Upvotes: 0

Views: 80

Answers (1)

Mike Pennington
Mike Pennington

Reputation: 43097

Side-by-side diff

To get a side-by-side diff, use the linux sdiff command.

$ sdiff before.txt after.txt
hostname Foo                                                  | hostname Bar
!                                                               !
interface GigabitEthernet1/1                                    interface GigabitEthernet1/1
 ip address 192.0.2.1 255.255.255.0                           |  ip address 192.0.2.254 255.255.255.0
$

IOS Diff

However, what you often want with Cisco gear is a Cisco IOS diff... To get a Cisco IOS diff, use the ciscoconfparse2 Diff() object... (full disclosure: I am the author of ciscoconfparse2)

Cisco IOS Diff Example in Python

Example...

from ciscoconfparse2 import Diff

config_before = """!
hostname Foo
!
interface GigabitEthernet1/1
 ip address 192.0.2.1 255.255.255.0
!"""

config_after = """!
hostname Bar
!
interface GigabitEthernet1/1
 ip address 192.0.2.254 255.255.255.0
!"""

diff =  Diff(config_before, config_after)
for line in diff.get_diff():
    print(line)

When you run that, you'll get the following Cisco IOS commands required to convert the before_config to the after_config:

$ python diff_this.py
no hostname Foo
hostname Bar
interface GigabitEthernet1/1
  ip address 192.0.2.254 255.255.255.0
$

Upvotes: 1

Related Questions