Network Change & Configuration Management

Reply
Highlighted

How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 236
5469     0

I have a need to compare two device output snapshots (call them "before" and "after") in some easily human visible way.  The snapshots consist of a series of Cisco IOS "show xxx" commands, where the output is captured before some significant change is made, and then after.  Basically, the initiator wants to know if everything resumed to normal or if there were any "surprises".

 

NetMRI scripts support creating each output sequence via the "PRINT" and "ARCHIVE" script directives.  What's missing is some way to compare those side-by-side.

 

The API includes the "config_diff" function, but of course that's limited to comparing two collected and stored config revisions.  Is there a way to exploit the nice side-by-side UI diff function, but for two arbitrary files?

 

The only alternative that I've come up with is to export the two result files to another platform where some other graphical "diff" program is used to perform the side-by-side comparison.

 

TIA,

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

[ Edited ]
Adviser
Posts: 62
5470     0

This may take a bit of extra work, but just wanted to mention a possible workaround in 7.3.1 by using the import_custom_config api call.

 

You can either add a dummy IP address as static entry and once the device viewer is created, manually change the Device Type from "unknown" to "Switch".

 

You can then upload both snapshot configs to this dummy IP by using the following link format on a new Tab of the browser.

 

http://<NetMRI-IP-Address>/api/3.3/config_revisions/import_custom_config?DeviceID=<ID>&RunningConfig="<A>"&SavedConfig="<B>"

Where
<NetMRI-IP-Address> - Your Appliance IP-Address or fqdn name
<ID> - DeviceID created for the new device
<A> - Snapshot before
<B> - Snapshot after

 

 

Note: There is an easier way to do this. This api call will also work on the existing device's DeviceID, so if you want to upload configs to any existing InfraDevice's Device Viewer, the static entry step can be skipped. However, please make sure, you delete the uploaded configs later, to avoid a confusion in the future and also to avoid triggering of issues like "Config Diff" etc.

 

This will allow you to use the usual Compare function on the Config Explorer

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Adviser
Posts: 401
5470     0

Do you want to do this in the UI or are you looking at doing it externally?

 

Sif

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

 

I have a similar issue of wanting to compare "Before the change" and "After the change" show command outputs within a Python script (internal to NetMRI).  Then to save the compare in the job Archive as a separate file, which I don't know how to do.  I found the following code as a test that creates a nice html output.  Not sure of its limitations, but it is interesting.

 

I ran this Python in the sandbox, I don't see why it wouldn't run as a job as well.  This is still a work in progress Smiley Wink

 

Let me know if you've gotten anyplace with this.

 

import difflib
text1 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer
 eu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitor
 tellus. Aliquam venenatis. Donec facilisis pharetra tortor.  In nec
 mauris eget magna consequat convallis. Nam sed sem vitae odio
 pellentesque interdum. Sed consequat viverra nisl. Suspendisse arcu
 metus, blandit quis, rhoncus ac, pharetra eget, velit. Mauris
 urna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,
 suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl porta
 adipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristique
 enim. Donec quis lectus a justo imperdiet tempus."""
text1_lines = text1.splitlines()
text2 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer
 eu lacus accumsan arcu fermentum euismod. Donec pulvinar, porttitor
 tellus. Aliquam venenatis. Donec facilisis pharetra tortor. In nec
 mauris eget magna consequat convallis. Nam cras vitae mi vitae odio
 pellentesque interdum. Sed consequat viverra nisl. Suspendisse arcu
 metus, blandit quis, rhoncus ac, pharetra eget, velit. Mauris
 urna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,
 suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl porta
 adipiscing. Duis vulputate tristique enim. Donec quis lectus a justo
 imperdiet tempus. Suspendisse eu lectus. In nunc. """
text2_lines = text2.splitlines()
d = difflib.HtmlDiff()
print d.make_file(text1_lines, text2_lines)

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

So, I got the script running now as an internal job and will output the html comparison to the sandbox.  I don't see any other way to mimic the CCS Archive method in Python in order to add the file to the All.zip.

 

Maybe it will help someone, so my code is below.  Basically do a show command, then make changes to router (Nexus), then do the show command again, and compare.

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     true
#
# Script-Variables:
#
# Script-Timeout: 
#	300
#
# END-SCRIPT-BLOCK

import requests, json, re, time, datetime
from infoblox_netmri.easy import NetMRIEasy

import difflib

# This values will be provided by NetMRI before execution
defaults = {
   "api_url": api_url,
   "http_username": http_username,
   "http_password": http_password,
   "job_id": job_id,
   "device_id": device_id,
   "batch_id": batch_id
}

# Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:

    # Run these commands one time at the start
    BeforeChanges = easy.send_command('show ip route vrf all')
    BeforeChanges_lines = BeforeChanges.splitlines()

    DoCommand = easy.send_command('config t')
    DoCommand = easy.send_command('vlan 4')
    DoCommand = easy.send_command('name Lon-test4')
    DoCommand = easy.send_command('exit')
  
    DoCommand = easy.send_command('vrf context Lon-test4')
    DoCommand = easy.send_command('exit')

    DoCommand = easy.send_command('interface Vlan4')
    DoCommand = easy.send_command('vrf member Lon-test4')
    DoCommand = easy.send_command('no ip redirects')
    DoCommand = easy.send_command('ip address 2.2.2.2/30')
    DoCommand = easy.send_command('no shutdown')
    DoCommand = easy.send_command('exit')
    DoCommand = easy.send_command('exit')

    AfterChanges = easy.send_command('show ip route vrf all')
    AfterChanges_lines = AfterChanges.splitlines()
   

d = difflib.HtmlDiff()
with open('BeforeAfter_Compare.html', 'w') as f:
    f.write(d.make_file(BeforeChanges_lines, AfterChanges_lines, "BeforeChanges", "AfterChanges", True, 5))
f.close()
    
    

 

The html output will look something like this:

2019-02-11_22-10-06.jpg

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Adviser
Posts: 401
5470     0

Great Script and thanks for sharing

 

An option you can use "easy.log_message"

 

    easy.log_message('info', device.DeviceName)
easy.log_message('info', "my message")

 

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

Thanks Sif, that gets it part way there at least, by writting to the custom.log.  Too bad we can't change the extension on the file to html, or just add a new file as we wish.  So, at the end of the script, I changed out writing to a file for the following code:

 

    d = difflib.HtmlDiff()
    easy.log_message('info', device_devicename)
    easy.log_message('info', d.make_file(BeforeChanges_lines, AfterChanges_lines, "BeforeChanges", "AfterChanges", True, 5))

You may not want to put the device name at the top if keeping the log as html.

 

Thanks!

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Adviser
Posts: 401
5470     0

Ok, I forgot about ARCHIVE. Smiley Happy

 

Undocumented Feature Alert:  Note that this ZIP file only contains *.txt files.  That means it will not be created unless you change the ARCHIVE filename to something besides the default (which has a .log extension instead of .txt). 
Additionally, the most recently created ZIP file will also be available for download directly from the NetMRI, using this URL:http://<NetMRI_IP>/netmri/ccs/tx/common/GetArchive.tdf.
Regarding filenames…  It is perfectly legal and reasonable to combine the use of $ipaddress with other variables as well.  For example, a customer wanted to capture all the BGP routes for each VRF that was defined on his routers.  The solution was to use a Trigger with the VRF name in a variable ($vrfname), and then to include this variable in the ARCHIVE filename along with the IP address:
ARCHIVE ($ipaddress-$vrfname): show ip route vrf $vrfname bgp
This produced a nicely-named list of files like:
10.10.1.5-data.txt
10.10.1.5-oam.txt
10.10.1.5-finance.txt
10.100.5.1-data.txt
10.100.5.1-oam.txt
10.100.5-1-finance.txt
 
Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

But Sif, this is a Python script.  Are you telling me there is a way to ARCHIVE using Python?  CCS would have worked for this script except for that we needed the Diff functionality.  Or perhaps there is a way to call Python Lib within CCS?

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Adviser
Posts: 401
5470     0

LOL, the beauty of NetMRI you can use that ARCHIVE command in PERL and Python Smiley Happy

 

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

Okay Sif, I'm feeling you'll need to bust me back down to Techie here, please provide an example of the syntax for Python and Perl with the ARCHIVE statement -- Smiley Happy

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Adviser
Posts: 401
5470     0

 

Real simple Smiley Happy

CCS Version

ARCHIVE (startup): sh startup-config

 

PERL or Python Smiley Happy

 

    DoCommand = easy.send_command(ARCHIVE (startup): sh startup-config)
or
DoCommand = easy.log(ARCHIVE (startup): sh startup-config)

 Should work, I haven't test it, but of course now I will

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

Well, after some testing, the send_command with the ARCHIVE works great (with quotes, shown below), but no success with the log_message command.

 

# Creates an ARCHIVE file Perfectly in Pyton and shows in the Files tab:
BeforeChanges = easy.send_command('ARCHIVE (BeforeChanges): show ip route vrf all')

# This one doesn't work, tried various configurations, just adds to custom.log:
easy.log_message('info','ARCHIVE (device_devicename): d.make_file(BeforeChanges_lines, AfterChanges_lines, "BeforeChanges", "AfterChanges", True, 5)')

Ideally, I would like to create the Diff output as it's own ARCHIVE file.

...so close!

Re: How to leverage NetMRI internal config comparison function for other than config revisions?

Expert
Posts: 175
5470     0

Rajiv - Just discovering that the import_custom_config requires both Running and Saved params to be passed to work Smiley Sad

 

Would have been much more flexible if we could add one at a time.

 

-Lon.

Showing results for 
Search instead for 
Do you mean 

Recommended for You