RESTCONF Interface Status Checker (IOS-XE)¶
This script connects to a Cisco IOS-XE device (or DevNet sandbox) using RESTCONF
and retrieves interface information from the IETF YANG model:
ietf-interfaces:interfaces.
It prints:
- Interface name
- First IPv4 address (if present)
- Enabled status (True/False)
Finally, it prints a summary:
- "At least one interface is down" if any interface has
enabled == False - Otherwise: "All interfaces are up"
How it works (high level)¶
- Build RESTCONF URL:
https://<host>:<port>/restconf/data/ietf-interfaces:interfaces - Send HTTP GET with:
- Basic authentication (USER/PASS)
- YANG JSON headers:
Accept: application/yang-data+jsonContent-Type: application/yang-data+json
- Parse JSON response and iterate over:
r_json["ietf-interfaces:interfaces"]["interface"] - For each interface record:
- Print
record["name"] - If
record["ietf-ip:ipv4"]["address"]exists, printaddress[0]["ip"], else printNo IPv4 - Print
record["enabled"] - Track if any interface is disabled (
enabled == False)
Requirements¶
- Python 3.x
requestslibrary
pip install requests
Notes / Caveats¶
- Credentials are hardcoded (fine for labs, not recommended for production).
- If you hit TLS/certificate issues in a lab (self-signed cert), you may need:
requests.get(..., verify=False)(not recommended for production). - The script only prints the FIRST IPv4 address if multiple exist.
- RESTCONF must be enabled on the device and reachable on the given port.
Example Output¶
interface: GigabitEthernet1 10.0.0.1 status: True
interface: GigabitEthernet2 No IPv4 status: False
At least one interface is down
Script¶
import sys, requests
URL = "https://ios-xe-mgmt.cisco.com:9443"
USER = "root"
PASS = "Cisco0123"
url = URL + "/restconf/data/ietf-interfaces:interfaces"
headers = {
"content-type": "application/yang-data+json",
"accept": "application/yang-data+json",
}
try:
result = requests.get(url, auth=(USER, PASS), headers=headers)
r_json = result.json()
flagDown = 0
print(r_json.keys())
for record in r_json["ietf-interfaces:interfaces"]["interface"]:
print("{0:<35}".format("interface: " + record["name"]), end="")
if "address" in record["ietf-ip:ipv4"]:
print(
"{0:<15}".format(record["ietf-ip:ipv4"]["address"][0]["ip"]),
end="",
)
else:
print("{0:<15}".format("No IPv4"), end="")
print("{0:<9}".format("status: "), end="")
print(str(record["enabled"]))
if record["enabled"] == False:
flagDown = 1
print("")
if flagDown:
print("At least one interface is down")
else:
print("All interfaces are up")
except:
print("Exception: " + str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1]))
print("Error: " + str(result.status_code), result.text)