QuestionQ70

VerifiedOperations

An engineer needs to audit drift between the startup and running configurations through NETCONF. The engineer must retrieve both datastores and write a unified-diff file for review. Options may be used more than once.

Drag & Drop
from ncclient import manager
import difflib

device = {"host": "198.51.100.30", "port": 830, "username": "netops", "password": "Cisco12345", "hostkey_verify": False}

with manager.connect(**device) as m:
    rcfg = m.(="running")
    scfg = m.(="startup")
    running_text = rcfg.
    startup_text = scfg.
    diff = difflib.(
        startup_text.splitlines(keepends=True),
        running_text.splitlines(keepends=True),
        fromfile="startup", tofile="running"
    )
    with open("config_drift.diff", "w") as f:
        f.writelines(diff)
Explanation

NETCONF get-config reads a specified configuration datastore, so source="running" and source="startup" retrieve the two required configurations. ncclient RPC replies expose configuration content through data_xml. difflib.unified_diff generates unified-diff lines suitable for writelines; placing startup first and running second reports the change from startup to running.

Community Discussion

No comments yet. Be the first to start the discussion!