Programming samples

NAPALM tries to provide a common interface and mechanisms to push configuration and retrieve state data from network devices. This method is very useful in combination with tools like Ansible, which in turn allows you to manage a set of devices independent of their network OS.


These samples assume you have set up your virtual lab (see Setting up the lab), and that the ‘eos’ box is accessible via point 12443 on your machine. You should also have the sample configuration files saved locally.

Now that you have installed NAPALM (see Installation) and set up your virtual lab, you can try running some sample scripts to demonstrate NAPALM in action. You can run each of the scripts below by either pulling the files from the GitHub repository, or you can copy the content to a local script (e.g., and run it.

For people new to Python:

  • the script name should not conflict with any existing module or package. For example, don’t call the script

  • run a Python script with $ python

Load/Replace configuration

Create a file called in a folder with the following content:

# Sample script to demonstrate loading a config for a device.
# Note: this script is as simple as possible: it assumes that you have
# followed the lab setup in the quickstart tutorial, and so hardcodes
# the device IP and password.  You should also have the
# 'new_good.conf' configuration saved to disk.
from __future__ import print_function

import napalm
import sys
import os

def main(config_file):
    """Load a config for the device."""

    if not (os.path.exists(config_file) and os.path.isfile(config_file)):
        msg = "Missing or invalid config file {0}".format(config_file)
        raise ValueError(msg)

    print("Loading config file {0}.".format(config_file))

    # Use the appropriate network driver to connect to the device:
    driver = napalm.get_network_driver("eos")

    # Connect:
    device = driver(
        optional_args={"port": 12443},

    print("Opening ...")

    print("Loading replacement candidate ...")

    # Note that the changes have not been applied yet. Before applying
    # the configuration you can check the changes:

    # You can commit or discard the candidate changes.
        choice = raw_input("\nWould you like to commit these changes? [yN]: ")
    except NameError:
        choice = input("\nWould you like to commit these changes? [yN]: ")
    if choice == "y":
        print("Committing ...")
        print("Discarding ...")

    # close the session with the device.

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print('Please supply the full path to "new_good.conf"')
    config_file = sys.argv[1]

Run the script, passing the path to the new_good.conf file as an argument:

python ../sample_configs/new_good.conf