首页 > 解决方案 > (Python) Netmiko : 将路由器的配置与模板进行比较

问题描述

我想使用 netmiko 库在 txt 文件中的路由器配置和模板之间进行比较。

使用“show run”命令显示所有路由器配置,必须逐块进行比较,例如:命令输出中的“access-list snmp”块与模板中的“access-list snmp”块等等,因为某些 permet xxxx 在 3 个 ACL 中重复,我必须检查它们是否在这 3 个 ACL 中。

我不知道如何进行此比较,如果您有任何想法,请帮助我。

我试图用这段代码来做,但这里的比较是逐行完成的:

cisco = { 
   'device_type': 'cisco_ios', 
   'host': 'router', 
   'username': 'admin', 
   'password': 'cisco123', 
   } 
   
try
    ssh = ConnectHandler(**cisco)
    ssh.send_command('terminal length 0')
    output = ssh.send_command("show run")

except Exception as e:
    # exceptions

try:
            
    template_file = open("template.txt", "r")
    for l in file:     
        line = l.strip()
        if line not in output:
            f = open("ligne_not_in_config.txt", "a")
            f.write(l, "is not in config\n")

    except FileNotFoundError as e:
        # exceptions

标签: python-3.xnetworkingparamikonetmiko

解决方案


推荐阅读