Network Automation : Get_facts to retrieve device information

Network Automation NAPALM (Python networking scripts examples)

In this network automation example, we will use the get_facts function to retrieve device information. This is a simple example of collecting information from a device. If you don’t have a network setup for practice, try using routers from Sandbox. Some routers are available 24×7. It doesn’t require you to activate the lab. Search for labs that have an “Always on” tab on them. Once you open this lab, you will see the design part on the right side and details such as IP address, username, and password on the left side.

We will connect to a Cisco router which is hosted in the sandbox with the following details

IP Address : 131.226.217.143
Username : developer
Password: C1sco12345

We will import get_network_driver from the Napalm library and also the JSON library.

from napalm import get_network_driver
import json

Now connect to the Cisco router using the below code. The first line includes the network driver. The second line includes device details such as IP address, username and password, and the third line uses the “device.open()” method to connect to a router.

driver = get_network_driver('ios')
device = driver('131.226.217.143','developer','C1sco12345')
device.open()

The below line of code uses the command get_facts() to retrieve device information. Here, I’m also using JSON to format the output for better viewing.

print(json.dumps(device.get_facts(),indent=2))

Finally, close the connection to the device.

device.close()

Output

{
  "uptime": 15180.0,
  "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",
    "Loopback56",
    "Loopback100"
  ]
}

In the above code, get_facts displays information such as uptime, vendor, serial number, hostname and also includes other details.

Complete Code

from napalm import get_network_driver
import json
driver = get_network_driver('ios')
device = driver('131.226.217.143','developer','C1sco12345')
device.open()
print(json.dumps(device.get_facts(),indent=2))
device.close()
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s