NETCONF Python Script - Interfaces Data (IOS XE)¶
Overview¶
This script demonstrates how to use NETCONF with Python (ncclient) to connect to a Cisco IOS XE device and retrieve interfaces state data using YANG subtree filtering.
Specifically, it retrieves:
- Interface state data from
ietf-interfaces:interfaces-state
The script connects to the Catalyst 8000v DevNet Sandbox device using NETCONF over SSH (port 830).
Prerequisites¶
1. Network Access¶
- Active connection to the Cisco DevNet Sandbox VPN
- Reachable device IP:
10.10.20.48
2. Python Environment¶
Python 3.8+ recommended
Install required packages:
pip install ncclient lxml
3. Device Access Details¶
| Parameter | Value |
|---|---|
| Host | 10.10.20.48 |
| Port | 830 |
| Username | developer |
| Password | C1sco12345 |
Script Purpose¶
This script performs the following actions:
- Opens a NETCONF session to an IOS XE device
- Sends a
<get>request with a YANG subtree filter -
Requests only:
-
<interfaces-state> - Prints the raw XML response
Download Script¶
Click to download the script directly:
Download NETCONF_Interfaces_data.py
Full Script¶
#!/usr/bin/env python
from lxml import etree
from ncclient import manager
# Request information
username = "developer"
password = "C1sco12345"
hostname = "10.10.20.48"
port = 830
# Make NETCONF connection
device = manager.connect(
host=hostname, port=port,
username=username, password=password,
hostkey_verify=False, device_params={},
allow_agent=False, look_for_keys=False
)
# Define XML Filter Request (ietf-interfaces: interfaces-state)
xml_filter = """
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface/>
</interfaces-state>
"""
# Request data from device with XML Filter
netconf_response = device.get(('subtree', xml_filter))
# Print the XML Response
print('XML Response:')
print('-' * 80)
print(etree.tostring(netconf_response.data_ele, pretty_print=True).decode('utf-8'))
# Close NETCONF session
device.close_session()