NETCONF Python Script – Hostname & Version Retrieval (IOS XE)¶
Overview¶
This script demonstrates how to use NETCONF with Python (ncclient) to connect to a Cisco IOS XE device and retrieve basic device information using YANG subtree filtering.
Specifically, it retrieves:
- Device hostname
- IOS XE software version
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:
-
<hostname> <version>- Prints the raw XML response
- Parses and displays the extracted values in human‑readable form
Download Script¶
Click to download the script directly:
Download NETCONF_Hostname_Version_Retrieval.py
Full Script¶
#!/usr/bin/env python
from lxml import etree
from ncclient import manager
# Connection details
username = "developer"
password = "C1sco12345"
hostname = "10.10.20.48"
port = 830
# Establish NETCONF connection
device = manager.connect(
host=hostname,
port=port,
username=username,
password=password,
hostkey_verify=False,
allow_agent=False,
look_for_keys=False
)
# Define YANG subtree filter
xml_filter = """
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<version />
<hostname />
</native>
"""
# Send NETCONF get request
netconf_response = device.get(('subtree', xml_filter))
# Print raw XML response
print('XML Response:')
print('-' * 80)
print(etree.tostring(netconf_response.data_ele, pretty_print=True).decode('utf-8'))
# Namespace mapping for XPath queries
ns_find = {
None: "http://cisco.com/ns/yang/Cisco-IOS-XE-native",
}
# Extract values from XML
device_hostname = netconf_response.data.find(".//hostname", namespaces=ns_find).text
device_version = netconf_response.data.find(".//version", namespaces=ns_find).text
# Display parsed results
print('-' * 80)
print(f'Hostname: {device_hostname}')
print(f'Version: {device_version}')
print("\n")
# Close NETCONF session
device.close_session()
How It Works (Step‑by‑Step)¶
1. Establishing NETCONF Connection¶
device = manager.connect(...)
- Opens an SSH-based NETCONF session to port 830
- Disables host key checking (safe for lab use)
- Uses password authentication only
2. Building the YANG Subtree Filter¶
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<version />
<hostname />
</native>
This filter:
- Targets Cisco’s native IOS XE YANG model
- Requests only two elements
- Reduces payload size and improves efficiency
3. Sending the NETCONF Request¶
netconf_response = device.get(('subtree', xml_filter))
- Executes a NETCONF
<get>RPC - Applies subtree filtering
- Returns only the requested data
4. Printing Raw XML Output¶
This section prints the <data> portion of the NETCONF reply for inspection and debugging.
5. Parsing XML with Namespaces¶
ns_find = { None: "http://cisco.com/ns/yang/Cisco-IOS-XE-native" }
Cisco YANG models use XML namespaces. This mapping allows XPath queries to locate elements correctly.
6. Extracting Values¶
device_hostname = ...
device_version = ...
- Searches the XML tree for
<hostname>and<version> - Extracts their text values
7. Displaying Results¶
Example output:
Hostname: CAT8KV-1
Version: 17.12.02
How to Run¶
- Make sure VPN is connected
- Save script as
get_device_info.py - Run:
python3 get_device_info.py
Expected Output¶
You will see:
- Raw XML NETCONF response
- Parsed values:
Hostname: CAT8KV-1
Version: 17.12.02
Security Notes¶
hostkey_verify=Falseis acceptable for labs only- Do NOT hardcode credentials in production
- Use SSH keys and environment variables for real systems
References¶
- ncclient documentation: https://ncclient.readthedocs.io/
- Cisco IOS XE YANG Models
- DevNet Sandbox IOS XE