How do I compare a text file that contains a router's running config with a live running config? Python, Linux

Question:

Firsly, I am a beginner to this. I have to note that

So I want to compare a router’s running-config which I stored in a local machine in a text file to a current running-config that I want to print out in my linux terminal.. This is what I got so far. (THIS is written in VSCODE, so i can then open it via the linux terminal)

import difflib 
import netmiko

routerconnectinfo = { #create variable of information to store
    'device_type':'cisco_ios', #connect via ssh
    'host':'192.168.56.101', #ip address of the router
    'username':'christian', #username of the route
    'password':'romero', #password for router
    'secret':'chicken'
}

session = netmiko.ConnectHandler(**routerconnectinfo)
session.enable()

config = session.send_command("show running-config")
file = open("running_config_copied.txt", "w")
file.write(config)
file.close()

#HERE I WANT TO DO A COMMAND THAT ISSUES 'SHOW RUNNING-CONFIG' WHICH IS LIVE.. I THEN WANT TO COMPARE THAT
#TO THE TEXT FILE I CREATED AS SHOWN ABOVE


with open('running_config_copied.txt', 'r') as file1:
    diffs = difflib.ndiff(file1.readlines(), 'show running-config'())
    for diff in diffs:
        print(diff)

I am trying to figure out the next part which is to print a ‘show running-config’ command in my linux terminal and then that be compared to the one I stored.

Asked By: christian romero

||

Answers:

you can try:

with open('running_config_copied.txt', 'r') as file1:
    diffs = difflib.ndiff(file1.readlines(),session.send_command('show running-config'))
    for diff in diffs:
        print(diff)
Answered By: curiouslearner
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.