Package: conf-diff
IntroductionThis 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.FeaturesShows the configuration differences on the terminal window with colourful output.Generate a HTML output file as a comparison report.InstallationInstall this module from PyPI:pipinstallconf-diffUsage:PrerequisiteAs 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.cfgwas taken before the change andsbx-nxos-mgmt.cisco.com_ntp_after.cfgafter 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 theconf_diffmodule in your python script and instantiate a class object with both config output files as arguments.importconf_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 terminalprint(config_change.diff())This will display the colourful configuration difference on the terminal.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 asbx-nxos-mgmt.cisco.com_html_output.htmlin your current directory unless expected absolute path is specified.See the screenshot below for thesbx-nxos-mgmt.cisco.com_html_output.htmlExampleIn 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 theexampledirectory.fromnetmikoimportConnectHandlerimportconf_diffimporttime# List of hosts or deviceshosts_list=['sandbox-nxos-1.cisco.com']# For loop to run through all the devices in the 'hosts_list'forhostinhosts_list:device={"device_type":"cisco_nxos","ip":host,"username":"admin","password":"Admin_1234!","port":"22",}# Creating a network connection with the deviceprint(f"**** Connecting to{device['ip']}**** ...\n")net_connect=ConnectHandler(**device)# Sending 'show' command to the device to take first configuration snapshot before updating the deviceprint(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 changewithopen(f"{device['ip']}_before_config.cfg","w")asf:f.write(current_config)print(f"{device['ip']}_before_config.cfg has been saved ...\n")# List of configuration commands to the deviceprint(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 configurationconfig_update=net_connect.send_config_set(config_commands)# Sleep for 2 sec before take another configuration snapshottime.sleep(2)# Sending 'show' command to the device again to take another configuration snapshot after the changeprint(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 changewithopen(f"{device['ip']}_after_config.cfg","w")asf:f.write(updated_config)print(f"{device['ip']}_after_config.cfg has been saved ...\n")# Teardown the network connection with the devicenet_connect.disconnect()# To print the colourful output on the terminalconfig_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 filehtml_diff=conf_diff.ConfDiff(f"{device['ip']}_before_config.cfg",f"{device['ip']}_after_config.cfg","html_diff_output.html")html_diff.diff()IssuesPlease raise an issue or pull request if you find something wrong with this module.AuthorsMuhammad RafiLicenseCisco Sample Code License, Version 1.1