We have seen how we can leverage python’s Netmiko module to interact with devices programmatically. In this blog post, we will see how we can achieve similar results with the NAPALM library for network automation. The main advantage of using NAPALM over netmiko is that there is a lot of stuff and heavy lifting that is baked right into NAPALM so the user doesn’t need to care about underlying workflows. There is a clear abstraction between what the user needs to execute vs how and what NAPALM needs to do to achieve the end goal.
I am not going to cover the installation of NAPALM. You can find more information in the official docs here.
How to connect with devices using NAPALM
from napalm import get_network_driver
driver = get_network_driver('ios')
device = driver('sandbox-iosxe-latest-1.cisco.com', 'developer', 'C1sco12345')
device.open()
output = device.get_facts()
print(output)
device.close()
╰─ python3 NAPALM\ Script\ 1a.py ─╯
{'uptime': 110100, 'vendor': 'Cisco', 'os_version': 'Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.3.1a, RELEASE SOFTWARE (fc3)', 'serial_number': '9ESGOBARV9D', 'model': 'CSR1000V', 'hostname': 'csr1000v-1', 'fqdn': 'csr1000v-1.lab.devnetsandbox.local', 'interface_list': ['GigabitEthernet1', 'GigabitEthernet2', 'GigabitEthernet3', 'Loopback0', 'Loopback1001', 'Loopback1002', 'Loopback10000']}
- From napalm import get_network_driver that takes in the argument of the device type you want to connect to. In this case, we are connecting to ios based device.
- Supply the credentials.
- Open the connection to the device.
- Perform the task you need to with the device
- Close the connection.
If you see the above method “get_facts()”, it pulls a lot of data from the device without you needing to run individual commands and parsing it down into a common dataset. This single method does that all for you. NAPALM refers to these methods as “GETTERS” and there are a lot of GETTER methods that you can execute to make your job easier.
Here is the URL of all supported GETTER methods that you can execute across multiple platforms
https://napalm.readthedocs.io/en/latest/support/index.html#getters-support-matrix
ENHANCEMENT TO ABOVE SCRIPT:-
You see we have to open the connection and then also remember to close the connection for a device. There is a way by which we can let python handle that for us instead, it’s using a CONTEXT MANAGER. In the below code the statement with <> as device is CONTEXT MANAGER that handled the opening and closing of connections for you.
from napalm import get_network_driver
from rich import print
driver = get_network_driver('ios')
with driver('sandbox-iosxe-latest-1.cisco.com', 'developer', 'C1sco12345') as device:
print(device.get_facts())
╰─ python3 NAPALM\ Script\ 1a.py ─╯
{
'uptime': 111780,
'vendor': 'Cisco',
'os_version': 'Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.3.1a, RELEASE SOFTWARE (fc3)',
'serial_number': '9ESGOBARV9D',
'model': 'CSR1000V',
'hostname': 'csr1000v-1',
'fqdn': 'csr1000v-1.lab.devnetsandbox.local',
'interface_list': ['GigabitEthernet1', 'GigabitEthernet2', 'GigabitEthernet3', 'Loopback0', 'Loopback1001', 'Loopback1002', 'Loopback10000']
}
If you look carefully, NAPALM GETTERS are returning outputs as a dictionary. You could easily convert them into a JSON format if you desire so by using 1 extra line of code.
# keeping everything else same
import json
with driver('sandbox-iosxe-latest-1.cisco.com', 'developer', 'C1sco12345') as device:
output = device.get_facts()
print(json.dumps(output, indent=4))
╰─ python3 NAPALM\ Script\ 1a.py ─╯
{
"uptime": 112020,
"vendor": "Cisco",
"os_version": "Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.3.1a, RELEASE SOFTWARE (fc3)",
"serial_number": "9ESGOBARV9D",
"model": "CSR1000V",
"hostname": "csr1000v-1",
"fqdn": "csr1000v-1.lab.devnetsandbox.local",
"interface_list": [
"GigabitEthernet1",
"GigabitEthernet2",
"GigabitEthernet3",
"Loopback0",
"Loopback1001",
"Loopback1002",
"Loopback10000"
]
}
While there are all sorts of GETTER Methods available for so many platforms. It doesn’t mean you can’t execute the plain old show commands using NAPALM. Let’s see how
with driver('sandbox-iosxe-latest-1.cisco.com', 'developer', 'C1sco12345') as device:
print(device.cli(['show version', 'show ip arp']))
{
'show version': 'Cisco IOS XE Software, Version 17.03.01a\nCisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.3.1a, RELEASE SOFTWARE
(fc3)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2020 by Cisco Systems, Inc.\nCompiled Wed 12-Aug-20 00:16 by mcpre\n\n\nCisco IOS-XE software, Copyright (c)
2005-2020 by cisco Systems, Inc.\nAll rights reserved. Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License ("GPL") Version 2.0. The\nsoftware
code licensed under GPL Version 2.0 is free software that comes\nwith ABSOLUTELY NO WARRANTY. You can redistribute and/or modify such\nGPL code under the terms of GPL Version 2.0. For more
details, see the\ndocumentation or "License Notice" file accompanying the IOS-XE software,\nor the applicable URL provided on the flyer accompanying the IOS-XE\nsoftware.\n\n\nROM: IOS-XE
ROMMON\n\ncsr1000v-1 uptime is 1 day, 7 hours, 12 minutes\nUptime for this control processor is 1 day, 7 hours, 13 minutes\nSystem returned to ROM by reload\nSystem image file is
"bootflash:packages.conf"\nLast reload reason: reload\n\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export,
transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and
users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with
U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found
at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\nexport@cisco.com.\n\nLicense Level: ax\nLicense Type:
N/A(Smart License Enabled)\nNext reload license Level: ax\n\nThe current throughput level is 1000 kbps \n\n\nSmart Licensing Status: UNREGISTERED/No Licenses in Use\n\ncisco CSR1000V (VXE)
processor (revision VXE) with 715705K/3075K bytes of memory.\nProcessor board ID 9ESGOBARV9D\nRouter operating mode: Autonomous\n3 Gigabit Ethernet interfaces\n32768K bytes of non-volatile
configuration memory.\n3978436K bytes of physical memory.\n6188032K bytes of virtual hard disk at bootflash:.\n\nConfiguration register is 0x2102',
'show ip arp': 'Protocol Address Age (min) Hardware Addr Type Interface\nInternet 10.10.20.28 16 0050.56bf.490f ARPA GigabitEthernet1\nInternet
10.10.20.48 - 0050.56bf.78ac ARPA GigabitEthernet1\nInternet 10.10.20.254 192 0050.56bf.d636 ARPA GigabitEthernet1'
}