Skip to content
Guides & References

Write an SIE script with NAPALM

How to use NAPALM in an SIE script: one get_config call across vendors, the option to remove secrets before storing, and a minimal script.

Script Integration EngineUpdated
On this page

Write an SIE script with NAPALM

NAPALM gives several vendors one Python interface. get_config() returns the running, startup and candidate configs the same way whatever the platform, so one script covers a mixed estate. Its sanitized option removes passwords and keys before you store anything.


Good for

  • Mixed estates on NAPALM’s core drivers: ios, iosxr (and iosxr_netconf), nxos and nxos_ssh, eos, junos. Community drivers add more.
  • Backups that shouldn’t contain secrets.
  • Collecting the startup config as well as the running one, to spot unsaved changes.

Set it up

Terminal window
sudo /opt/rconfig-sie/venv/bin/pip install napalm
sudo -u apache /opt/rconfig-sie/venv/bin/python -c "import napalm" # www-data on Ubuntu

NAPALM pulls in each vendor’s own library (Netmiko, PyEZ, pyeapi and others), so it is a large install.


The command

The first argument is the NAPALM driver name:

/opt/rconfig-sie/venv/bin/python {script_path}my-napalm.py ios {device_ip} {device_username} {device_password} {device_enable_password}

A minimal script

napalm-skeleton.py
#!/usr/bin/env python3
"""Minimal SIE script with NAPALM: running config, secrets removed.
Usage:
napalm-skeleton.py <driver> <ip> <username> <password> [enable_password]
"""
import sys
from napalm import get_network_driver
def fail(message):
print(f"ERROR: {message}", file=sys.stderr)
sys.exit(1)
def main():
if len(sys.argv) < 5:
fail("usage: napalm-skeleton.py <driver> <ip> <username> <password> [enable_password]")
driver_name, host, username, password = sys.argv[1:5]
secret = sys.argv[5] if len(sys.argv) > 5 else ""
try:
driver = get_network_driver(driver_name)
except Exception as exc:
fail(f"unknown NAPALM driver '{driver_name}': {exc}")
optional_args = {"secret": secret} if secret else {}
device = driver(hostname=host, username=username, password=password,
timeout=60, optional_args=optional_args)
try:
device.open()
configs = device.get_config(retrieve="running", sanitized=True)
except Exception as exc: # each driver raises its own errors
fail(f"{type(exc).__name__}: {exc}")
finally:
try:
device.close()
except Exception:
pass
config = configs.get("running", "")
if not config.strip():
fail(f"no running config from {host}")
sys.stdout.write(config.rstrip("\n") + "\n")
print(f"SIE-OK: napalm {driver_name} {len(config.splitlines())} lines from {host}", file=sys.stderr)
return 0
if __name__ == "__main__":
sys.exit(main())

Habits that matter with NAPALM

  • sanitized=True. Removes passwords, keys and SNMP communities using patterns for each driver. Check the result on your platforms: it only removes what the driver knows about.
  • retrieve="running". "all" also fetches startup and candidate, which takes longer. Fetch them only when you’ll use them.
  • Close the connection in finally. Some drivers hold a session open on the device until closed.

Type to search.