Skip to main content

compare configuration files

Project description

license Pypi Build Status published

Introduction

This module is built to provide you the configuration comparison between two configuration files and generates configuration differences either on the terminal or create a HTML output file based on the parameter provided to the module.

Note: This module is built on the top of the Python built-in difflib module but modified to show you the colourful output and customised HTML template.

Features

  • Shows the configuration differences on the terminal window with colourful output.
  • Generate a HTML output file as a comparison report.

Installation

Install this module from PyPI:

pip install conf-diff

Usage:

Prerequisite

As this module compares the configuration difference between two config file, so we need to have two configuration files should be present in the same directory where you are running the script from or specify the absolute path for the configuration files. e.g. "/Users/rafi/sandbox-nxos-1.cisco.com_before_config.cfg" and "/Users/rafi/sandbox-nxos-1.cisco.com_after_config.cfg"

You may use either .cfg or .txt file extensions.

In the below example, I am using two output files of 'show running-config ntp' from the Cisco NXOS always-on sandbox, assuming that, sbx-nxos-mgmt.cisco.com_ntp_before.cfg was taken before the change and sbx-nxos-mgmt.cisco.com_ntp_after.cfg after the change, and we want to see the configuration diffrence between them. You may name the filenames as you like or add the timestamp.

Import the conf_diff module in your python script and instantiate a class object with both config output files as arguments.

import conf_diff

# Instantiate a class object 'config_change'
config_change = conf_diff.ConfDiff("sbx-nxos-mgmt.cisco.com_ntp_before.cfg", "sbx-nxos-mgmt.cisco.com_ntp_after.cfg")

# Display the output of the configuration difference on the terminal 
print(config_change.diff())

This will display the colourful configuration difference on the terminal.

App Screenshot

To generate a html output file, add third argument as the expected output file name. e.g. "sbx-nxos-mgmt.cisco.com_html_output.html"

 # Instantiate a class object 'html_diff'
html_diff = conf_diff.ConfDiff("sbx-nxos-mgmt.cisco.com_ntp_before.cfg", "sbx-nxos-mgmt.cisco.com_ntp_after.cfg", "sbx-nxos-mgmt.cisco.com_html_output.html")

# Generates a `sbx-nxos-mgmt.cisco.com_html_output.html` in your current directory unless expected absolute path is specified.
html_diff.diff()

This will generates a sbx-nxos-mgmt.cisco.com_html_output.html in your current directory unless expected absolute path is specified.

See the screenshot below for the sbx-nxos-mgmt.cisco.com_html_output.html

App Screenshot

Example

In this example, I am running a script with well known 'netmiko' library and taking a backup of running config before and after the change. Then compare the configuration difference between these config files. See the example directory.

from netmiko import ConnectHandler
import conf_diff
import time

# List of hosts or devices
hosts_list = ['sandbox-nxos-1.cisco.com']

# For loop to run through all the devices in the 'hosts_list'
for host in hosts_list:
    device = {
        "device_type": "cisco_nxos",
        "ip": host,
        "username": "admin",
        "password": "Admin_1234!",
        "port": "22",
    }

    # Creating a network connection with the device
    print(f"**** Connecting to {device['ip']} **** ...\n")
    net_connect = ConnectHandler(**device)

    # Sending 'show' command to the device to take first configuration snapshot before updating the device
    print(f"Connected to {device['ip']}, Sending commands ...\n")
    current_config = net_connect.send_command("show running-config")

    print(f"Saving pre-configuration change output for {device['ip']} ...\n")

    # Opening a file in write mode to save the configuration before the change
    with open(f"{device['ip']}_before_config.cfg", "w") as f:
        f.write(current_config)

    print(f"{device['ip']}_before_config.cfg has been saved ...\n")

    # List of configuration commands to the device
    print(f"Updating the configuration for {device['ip']}...\n")
    config_commands = ['interface Ethernet1/22-28',
                       'description testing python script',
                       'switchport mode trunk',
                       'switchport trunk allowed vlan 512,654,278'
                       ]

    # Sending above configuration commands to updathe the device configuration
    config_update = net_connect.send_config_set(config_commands)

    # Sleep for 2 sec before take another configuration snapshot
    time.sleep(2)

    # Sending 'show' command to the device again to take another configuration snapshot after the change
    print(f"Saving post-configuration change output for {device['ip']} ...\n")
    updated_config = net_connect.send_command("show running-config")

    # Opening a file in write mode to save the configuration after the change
    with open(f"{device['ip']}_after_config.cfg", "w") as f:
        f.write(updated_config)

    print(f"{device['ip']}_after_config.cfg has been saved ...\n")

    # Teardown the network connection with the device
    net_connect.disconnect()

    # To print the colourful output on the terminal
    config_diff = conf_diff.ConfDiff(f"{device['ip']}_before_config.cfg", f"{device['ip']}_after_config.cfg")
    print(config_diff.diff())

    # To generate a HTML output file
    html_diff = conf_diff.ConfDiff(f"{device['ip']}_before_config.cfg", f"{device['ip']}_after_config.cfg", "html_diff_output.html")
    html_diff.diff()

Issues

Please raise an issue or pull request if you find something wrong with this module.

Authors

Muhammad Rafi

License

Cisco Sample Code License, Version 1.1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

conf_diff-0.6.5.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

conf_diff-0.6.5-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file conf_diff-0.6.5.tar.gz.

File metadata

  • Download URL: conf_diff-0.6.5.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for conf_diff-0.6.5.tar.gz
Algorithm Hash digest
SHA256 e223b8410dd584a90715636029f9613df993f9782e69f220323f4c5a60a8a6fc
MD5 ab1b023d79bedf59996ea4f1f09e4fbe
BLAKE2b-256 391ef5cc93466b7bdfeb7b5c65f320978830d07186cab80dd47772d0ba0da7a9

See more details on using hashes here.

File details

Details for the file conf_diff-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: conf_diff-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for conf_diff-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 26f9ca41b8b899363691d43ed7fca547a2cbdb17b3261ce4b9a845455937f794
MD5 2e562a322b5e0db90a89caee05edab93
BLAKE2b-256 76e5b7b052df2dfd2efb3965a018c4be99e1cb9d44b4f0802a33215105368487

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page