如何将包含路由器运行配置的文本文件与实时运行配置进行比较?Python,Linux

3wabscal  于 2022-11-22  发布在  Linux
关注(0)|答案(1)|浏览(79)

首先,我是一个初学者。我必须注意到
所以我想比较一个路由器的running-config,我存储在一个本地机器的文本文件中,我想在我的linux终端打印出来的当前running-config..这是我目前得到的.(这是写在VSCODE,所以我可以通过linux终端打开它)

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)

我试图找出下一部分,这是打印一个'显示运行配置'命令在我的Linux终端,然后将其与我存储的比较。

ctehm74n

ctehm74n1#

您可以尝试:

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)

相关问题