Using the AOS-CX NAPALM Drivers
Python is one of the most widely used automation scripting languages, and it is incredibly popular among network automation communities. This makes NAPALM automation accessible to a huge portion of network automation engineers. Below is the way to get started with a simple NAPALM Python script. This section can be ran in Python's built-in IDLE interpreter, or within a .py Python file.
Beginning the Python script
Start the script by importing the NAPALM library. Optionally, the 'json' library can be imported for readability
import napalm
import json
Additionally, if no private certificates were installed on the device, the script may receive the SSL warning 'InsecureRequestWarnings' due to an unverified HTTPS request to the host. If the user wants the script to hide such warnings, add these lines as well.
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Declare and Instantiate the AOS-CX driver and other variables
The next line will use the NAPALM get_network_driver function to use the AOS-CX driver. This specifies that all calls with the 'driver' variable will utilize the AOS-CX version of the object.
driver = napalm.get_network_driver('aoscx')
It would also help to create variables for the username and password to authenticate with the switch. Replace the 'username' and 'password' text with the credentials needed to connect to the device. Also replace the '10.10.10.100' with the IP address of the device.
myuser = 'username'
mypass = 'password'
ipaddress = '10.10.10.100'
Loop through multiple function calls
Choose a few of the supported functions from the list specified here to be used in this script. This example will use the get_facts() function and the get_lldp_interfaces() function.
with driver(ipaddress, myuser , mypass ) as device:
print(json.dumps(device.get_facts(), sort_keys=True))
print(json.dumps(device.get_lldp_neighbors(), sort_keys=True))
This loop attempts to connect with the device at the 'ipaddress' using the 'myuser' and 'mypass' variables for the credentials, based off the information specified above.
It then performs a statement line to print the output of the functions for 'device.get_facts()' and 'device.get_lldp_neighbors()'.
By using the json.dumps function with the 'sort_keys=True' parameter, this will organize the JSON output in a sorted, readable manner.
Full Example Script
import napalm
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
driver = napalm.get_network_driver('aoscx')
myuser = 'admin'
mypass = 'admin'
ipaddress = '15.129.81.208'
with driver(ipaddress, myuser , mypass ) as device:
print(json.dumps(device.get_facts(), sort_keys=True))
print(json.dumps(device.get_lldp_neighbors(), sort_keys=True))
Example Output
{"fqdn": "NAE-8320-01", "hostname": "NAE-8320-01", "interface_list": ["1/1/53:1", "1/1/31", "1/1/23", "1/1/44", "1/1/1", "1/1/3", "1/1/39", "1/1/51:4", "1/1/17", "1/1/11", "1/1/37", "1/1/47", "1/1/36", "1/1/27", "1/1/45", "1/1/32", "1/1/52:3", "1/1/53:4", "1/1/5", "1/1/21", "1/1/41", "1/1/33", "1/1/15", "1/1/50", "1/1/51:1", "1/1/24", "1/1/43", "1/1/6", "1/1/51:2", "1/1/46", "1/1/35", "1/1/50:2", "1/1/28", "1/1/50:3", "1/1/50:4", "1/1/14", "1/1/18", "1/1/49:1", "1/1/53:3", "1/1/29", "1/1/2", "1/1/49:4", "1/1/53:2", "1/1/53", "1/1/52:4", "1/1/54", "1/1/22", "1/1/16", "1/1/40", "1/1/42", "1/1/20", "1/1/50:1", "1/1/34", "1/1/10", "1/1/12", "1/1/52", "1/1/54:4", "1/1/25", "1/1/51", "1/1/49:2", "1/1/54:2", "1/1/54:3", "1/1/19", "1/1/48", "1/1/7", "1/1/9", "1/1/13", "1/1/49", "1/1/30", "1/1/26", "1/1/54:1", "1/1/52:1", "1/1/38", "1/1/49:3", "1/1/8", "1/1/4", "1/1/51:3", "1/1/52:2", "vlan1010", "vlan302", "loopback1", "vlan100", "vlan1020", "vlan486"], "model": "8320", "os_version": "ArubaOS-CX:TL.10.05.0001:53cb98af4936:202007092355", "serial_number": "TW83KCW011", "uptime": 1596129.947, "vendor": "Aruba"}
{"1/1/1": [{"hostname": "tor-sw-6-7", "port:": "5"}], "1/1/31": [{"hostname": "VOIP-Auto-2930M", "port:": "23"}], "1/1/32": [{"hostname": "NAE-2930M-02", "port:": "23"}], "1/1/46": [{"hostname": "NAE-8320-02", "port:": "1/1/46"}], "1/1/47": [{"hostname": "NAE-8320-02", "port:": "1/1/47"}], "1/1/48": [{"hostname": "NAE-8320-02", "port:": "1/1/48"}]}
The Power of NAPALM
NAPALM is used to abstract the differences among the various switch operating systems, and it is quite easy to show that in action. A user can simply change the variable declaration 'driver = napalm.get_network_driver('aoscx')' to another supported operating system, and change the 'ipaddress' (and credentials) to point to a completely different device, and the rest of the code will function similarly!
Updated over 1 year ago