Napalm Python


Introduction

Napalm is a powerful Python library that automates and interacts with network devices. It provides an easy-to-use platform to programmatically configure and manage network devices, such as routers and switches, using native protocols like NETCONF and SSH. Napalm supports various operating systems, including Cisco IOS, Arista EOS, Juniper Junos, etc.

It also provides an abstract interface allowing users to write code without worrying about device-specific configuration, making Napalm easier for network engineers and developers to quickly develop scripts that can configure thousands of devices from different vendors.

Napalm also provides an API that allows users to write custom scripts and applications that interact with the devices. That enables users to develop new tools and applications rapidly. For example, a network engineer could use Napalm to quickly develop an application for monitoring the performance of their devices or for identifying and troubleshooting problems.


Pre-requisite

  • Python

 

Example

In this example, we will check the route of a cisco router  using Napalm. First, we need to install the library:

        pip install napalm
        
Once the installation is complete, we can start writing our script. Let's import the necessary libraries first:

  from napalm import get_network_driver
  # Next, we can define our parameters for connecting to the router: 
  driver = get_network_driver('ios') 
  device = driver(hostname='cisco_router', username='admin',password='password') 
  device.open()
  #Finally, we can use the get_route() method to retrieve the route table of the router: 
  routes = device.get_route()
  for route in routes:
    print(route)
  #When we are done, we can close the connection to the router:
  device.close() 
Using Napalm, we can quickly and easily retrieve information from our network devices, making it a valuable tool for network engineers who need to manage large networks of multivendor devices.

Napalm's abstract interface makes it easy to write portable code across multiple operating systems and vendors.


More commands

This command will return route information

iosRouter.get_route_to(destination='6.6.6.6')

This command will return counters for all the router's interfaces, and with the last line, we printed the number of transmitted broadcast packets on the Ethernet1/2 interface.

intOutput = iosRouter.get_interfaces_counters()
print(json.dumps(intOutput,indent = 2))
print(intOutput['Ethernet1/2']['tx_broadcast_packets'])

The get_arp_table() retrieves the ARP table from a device. This table contains information about the IP and MAC addresses of devices connected to the router.
showArp = iosRouter.get_arp_table()
print(json.dumps(showArp,indent = 2))
      
The ping() method can be used to test network connectivity. By providing an IP address, it will return a dictionary containing information about the success of the ping, such as the round trip time and packet loss rate.
print(json.dumps(iosRouter.ping('4.4.4.4'),indent = 2))

The traceroute() method can be used to trace the route of a packet from one device to another. It will return a list containing dictionaries that contain information about each hop in the path, such as the hostname and IP address of the router along with its round trip time.

print(json.dumps(iosRouter.traceroute('3.3.3.3'),indent = 2))

The get_config() command can be used to retrieve the entire configuration of a device. This includes all the commands that are used to configure a device and enable its features. The data is returned as a string and can be parsed for further analysis.
 print(json.dumps(iosRouter.get_config(),indent= 2))

Finally, the get_interfaces() command can be used to retrieve a list of all interfaces on a device. This information can then be used for further analysis and automation tasks.
print(json.dumps(iosRouter.get_interfaces(),indent = 2))


 

Thank you for reading. I hope this has been helpful in understanding the capabilities of Napalm and how it can help in network automation.
If you need any help, don't hesitate to reach out.  We are more than happy to help you in any way we can.

Talk to an expert

List of titles