NAPALM Network Automation: Getters, Config Diff and Load - 夜莺博客

NAPALM Network Automation: Getters, Config Diff and Load

Writing one script per vendor - SSH to Cisco with one parser, NETCONF to Juniper with another - does not scale. NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) gives you one Python API across IOS, IOS-XE, NX-OS, EOS, Junos and more: getters return structured dictionaries instead of raw CLI text, and configuration changes run through a safe load-diff-commit cycle with rollback. This guide covers the NAPALM essentials with working code samples.

Installing NAPALM and Opening a Device

pip install napalm
import napalm

driver = napalm.get_network_driver("ios")  # or eos, junos, nxos, iosxr
with driver(hostname="192.168.1.1", username="admin",
            password="cisco123", optional_args={"port": 22}) as device:
    facts = device.get_facts()
    print(facts["hostname"], facts["model"], facts["os_version"])

The context manager closes the connection automatically. Behind the scenes NAPALM uses Netmiko over SSH for IOS-style platforms and NETCONF for Junos - you do not need to care which, the API is identical.

Reading State with Getters

with driver(hostname="10.0.0.11", username="net", password="secret") as d:
    d.open()
    bgp = d.get_bgp_neighbors()
    interfaces = d.get_interfaces()
    vlans = d.get_vlans()
    config = d.get_config()
    print(config["running"])

Useful getters include get_facts(), get_interfaces(), get_lldp_neighbors(), get_arp_table(), get_mac_address_table(), get_bgp_neighbors() and get_environment() for power/fan/temperature. Getters that a driver does not implement raise NotImplementedError - check the support matrix and handle it.

Configuration Changes: Load, Diff, Commit, Rollback

NAPALM treats config changes like a transaction - nothing is applied until you commit:

with driver(hostname="10.0.0.11", username="net", password="secret") as d:
    d.open()
    d.load_merge_candidate(filename="snmp.cfg")   # overlay changes
    # d.load_replace_candidate(filename="full.cfg")  # full replace
    print(d.compare_config())   # review the diff BEFORE committing
    d.commit_config()           # atomic apply
    # d.rollback()              # revert the last commit if needed

load_merge_candidate overlays only your snippet; load_replace_candidate makes the device end in exactly the supplied state and can wipe unrelated configuration if the candidate is incomplete - always run compare_config() first. On supported platforms rollback() reverts the last commit, and discard_config() throws away an uncommitted candidate.

Beyond Basics

NAPALM also offers get_network_driver("junos") NETCONF-based flows, ping()/traceroute() helpers, and a compliance_report() that validates device state against a source of truth. Pair it with Nornir for parallel execution across the fleet, or with Ansible via the napalm.napalm collection for inventory-driven playbooks.

Related Guides on This Site

Start with the foundations: Netmiko for SSH automation, Ansible multi-vendor command modules, and NETCONF vs RESTCONF vs gNMI to choose the right northbound API.

原文链接:https://napalm.readthedocs.io/en/latest/tutorials/samples.html