[Python] (Python) Netmiko : Compare the configuration of a router with a template

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to do a comparison between a router configuration and template which is in a txt file using netmiko library.

    using the "show run" command to display all the router configuration, the comparison must be done block by block for example: the "access-list snmp" block from the output of the command with the "access-list snmp" block from template etc because some permet x.x.x.x are repeated in 3 ACL and I have to check that they are in these 3 ACL.

    I don't know how to do this comparison, if you have any ideas please help me.

    I tried to do it with this code but here the comparison is done line by line :

    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

    Continue reading...

Compartilhe esta Página