Challenge
Problem
Your colleague has prepared an automation script to check the status of network interfaces. The script uses the DNA Center API to
retrieve the status of network interfaces and display the device name, port name, and IP address of the interfaces that are up.
However, the script is not working as expected. Your task is to fix the script so that it displays the correct information and
only for the Vlan interfaces.
The script is provided below - can you spot the mistakes and fix them?
HINT: The script uses the DNA Center API to retrieve the network interfaces and devices. The interfaces are stored in the
interfaces list, and the devices are stored in the devices list. The script loops over the interfaces and devices to get
the device name, port name, and IP address of the interfaces that are up. The script uses the message template to format the
output. The vlan_interfaces list is used to store the Vlan interfaces. The script prints the status of the interfaces instead
of the formatted message. The two(2) mistakes are after line 50. Read the whole thing carefully, line by line, and it should be
clear where the mistakes are.
The desired output looks like this:
Desired OutputPassword:
Device Name: Switch1.ciscotest.com
Port Name: Vlan101
IP Address: 172.16.101.254
Device Name: Switch1.ciscotest.com
Port Name: Vlan1
IP Address: None
Device Name: switch2.ciscotest.com
Port Name: Vlan1
IP Address: 192.168.0.1
Device Name: switch2.ciscotest.com
Port Name: Vlan101
IP Address: 172.16.101.253
Device Name: switch3.ciscotest.com
Port Name: Vlan101
IP Address: 172.16.101.251
Device Name: switch3.ciscotest.com
Port Name: Vlan1
IP Address: None
Actual OutputPassword:
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
up
| Catalyst Center Interfaces Script |
|---|
| from getpass import getpass
import requests
import urllib3
# SET A BASE URL
BASE_URL = "https://sandboxdnac2.cisco.com"
# Derived URLs
auth_url = BASE_URL + "/dna/system/api/v1/auth/token"
interface_url = BASE_URL + "/dna/intent/api/v1/interface"
device_url = BASE_URL + "/dna/intent/api/v1/network-device"
# disable warnings for self-signed certificate
urllib3.disable_warnings() # disable SSL warnings requests
# credentials
username = "devnetuser"
password = getpass() or "Cisco123!"
auth = (username, password)
# create a session
session = requests.Session()
# request the token
response = requests.post(
url=auth_url,
auth=auth,
verify=False, # disable SSL verification (self-signed certificate)
)
# the token set in the header
token = response.json()["Token"]
session.headers.update({"x-auth-token": token})
# get the interfaces
response = session.get(interface_url, verify=False)
interfaces = response.json()["response"]
# define the message template to print on the console
message = """
Device Name: {device_name}
Port Name: {portname}
IP Address: {ip_address}
"""
# devices list
response = session.get(device_url, verify=False)
devices = response.json()["response"]
# create a list of vlan interfaces
vlan_interfaces = []
for interface in interfaces:
if interface["portName"].startswith("Vlan"):
vlan_interfaces.append(interface)
# loop over the vlan interfaces
for interface in interfaces:
# get the device id, ip address, port name
port_device_id = interface["deviceId"]
ip_address = interface["ipv4Address"]
portname = interface["portName"]
# get the device name from the devices list
for device in devices:
if device["id"] == port_device_id:
device_name = device["hostname"]
break
# format the message with data from the interface and the device name
status = message.format(
device_name=device_name,
portname=portname,
ip_address=ip_address,
)
# print the message
print(interface["status"])
|
Solution
Click to expand!
| Solution |
|---|
| from getpass import getpass
import requests
import urllib3
# SET A BASE URL
BASE_URL = "https://sandboxdnac2.cisco.com"
# Derived URLs
auth_url = BASE_URL + "/dna/system/api/v1/auth/token"
interface_url = BASE_URL + "/dna/intent/api/v1/interface"
device_url = BASE_URL + "/dna/intent/api/v1/network-device"
# disable warnings for self-signed certificate
urllib3.disable_warnings() # disable SSL warnings requests
# credentials
username = "devnetuser"
password = getpass() or "Cisco123!"
auth = (username, password)
# create a session
session = requests.Session()
# request the token
response = requests.post(
url=auth_url,
auth=auth,
verify=False, # disable SSL verification (self-signed certificate)
)
# the token set in the header
token = response.json()["Token"]
session.headers.update({"x-auth-token": token})
# get the interfaces
response = session.get(interface_url, verify=False)
interfaces = response.json()["response"]
# define the message template to print on the console
message = """
Device Name: {device_name}
Port Name: {portname}
IP Address: {ip_address}
"""
# devices list
response = session.get(device_url, verify=False)
devices = response.json()["response"]
# create a list of vlan interfaces
vlan_interfaces = []
for interface in interfaces:
if interface["portName"].startswith("Vlan"):
vlan_interfaces.append(interface)
# loop over the vlan interfaces
for interface in vlan_interfaces:
# get the device id, ip address, port name
port_device_id = interface["deviceId"]
ip_address = interface["ipv4Address"]
portname = interface["portName"]
# get the device name from the devices list
for device in devices:
if device["id"] == port_device_id:
device_name = device["hostname"]
break
# format the message with data from the interface and the device name
status = message.format(
device_name=device_name,
portname=portname,
ip_address=ip_address,
)
# print the message
print(status)
|