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.

Note

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., sample_napalm_script.py) 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 napalm.py.
  • run a Python script with $ python your_script_name.py.

Load/Replace configuration

Create a file called load_replace.py 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(hostname='127.0.0.1', username='vagrant',
                    password='vagrant', optional_args={'port': 12443})

    print('Opening ...')
    device.open()

    print('Loading replacement candidate ...')
    device.load_replace_candidate(filename=config_file)

    # Note that the changes have not been applied yet. Before applying
    # the configuration you can check the changes:
    print('\nDiff:')
    print(device.compare_config())

    # You can commit or discard the candidate changes.
    try:
        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 ...')
        device.commit_config()
    else:
        print('Discarding ...')
        device.discard_config()

    # close the session with the device.
    device.close()
    print('Done.')


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

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

python load_replace.py ../sample_configs/new_good.conf