Skip to content

Napalm Network Automation Basics

Napalm Network Automation Basics#

Getting Started#

Install the prerequisites

  • Python
  • Virtualbox
  • Vagrant

Install napalm

pip install napalm

Getting Facts from a device#

Import napalm

import napalm

Get the network driver

driver = napalm.get_network_driver("junos")

You would want to get this from cmdb rather

Use the driver like a function to get a handle on a specific device

device = driver(hostname="vqfx1", username="antidote", password="antidotepassword")

Initiate the connection with:

Get facts of the device:


Get Interfaces on a device#

interfaces = device.get_interfaces()

print the interfaces


You can loop through this response to see which interfaces are up or down:

for if_name, if_properties in interfaces.items():
    if if_properties['is_up']:
        print("Interface %s is UP" % if_name)
        print("Interface %s is DOWN" % if_name)

Get layer 3 information with:

layer3 = device.get_interfaces_ip()

See a full list of functions supported by napalm

Command Line Utility#

You can create an alias for brevity

alias napalm="napalm --user=antidote --password=antidotepassword --vendor=junos"

Then you can run all napalm functions directly from the cli

napalm vqfx1 call get_interfaces

Use bash utilities to pipe response and find a specific interface

napalm vqfx1 call get_interfaces | jq .em4

Execute a ping (reacability test):

napalm vqfx1 call ping --method-kwargs="destination=''"

Commiting configurations#

    import napalm

    vqfx1_config = """
                    <description>This is em0, and it connects to something.</description>

    driver = napalm.get_network_driver("junos")
    device = driver(hostname="vqfx1", username="antidote", password="antidotepassword")

    # Load device config

    # Compare the device config

    # Discard the config
    # device.commit_config()

    # Commit config

    # Roll back an update