IOS-XR routing table via netconf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 04:42 AM
Hi,
I'm pretty new at IOS-XR and Netconf and can't google it myself. How could I get the current routing table of an Cisco IOS-XR router via netconf (ncclient). I want the output from *show ip route*, but in a structured format.
I think I have to use the get method and a filter, but I could not figure out how to create the filter for that. Can someone please help me? I really don't want to parse the routing table via regex
- Labels:
-
Other Routing

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 05:11 AM
You could use https://github.com/YangModels/yang/blob/main/vendor/cisco/xr/600/Cisco-IOS-XR-ip-rib-ipv4-oper.yang the code would look like below (update for your needs)
from ncclient import manager
import xml.etree.ElementTree as ET
# --- Connection Parameters ---
HOST = 'your_ios_xr_device'
PORT = 830
USER = 'your_username'
PASS = 'your_password'
# --- YANG Filter for Specific Data ---
filter_rib_entries = """
<filter>
<rib xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ip-rib-ipv4-oper">
<vrfs>
<vrf>
<vrf-name>default</vrf-name>
<afs>
<af>
<af-name>ipv4-unicast</af-name>
<safs>
<saf>
<saf-name>ipv4-unicast</saf-name>
<ip-rib-route-table-names>
<ip-rib-route-table-name>default</ip-rib-route-table-name>
<routes>
<route>
<route-name>192.168.1.0/24</route-name>
</route>
</routes>
</ip-rib-route-table-names>
</saf>
</safs>
</af>
</afs>
</vrf>
</vrfs>
</rib>
</filter>
"""
def get_rib_entries():
with manager.connect(host=HOST, port=PORT, username=USER, password=PASS,
hostkey_verify=False) as m:
# --- Get Config ---
response = m.get_config(source='running')
if response.errors:
raise Exception("Configuration Retrieval Error:", response.errors)
else:
print("Configuration retrieval successful!")
# --- Get Operational Data (RIB Entries) ---
response = m.get(('subtree', filter_rib_entries))
if response.errors:
raise Exception("RIB Entry Retrieval Error:", response.errors)
# --- Process XML Response ---
rib_data = ET.fromstring(response.data_xml)
for route in rib_data.findall(".//{http://cisco.com/ns/yang/Cisco-IOS-XR-ip-rib-ipv4-oper}route"):
print(f"Route: {route.find('.//{http://cisco.com/ns/yang/Cisco-IOS-XR-ip-rib-ipv4-oper}route-name').text}")
# Extract and process other route attributes as needed
if __name__ == "__main__":
try:
get_rib_entries()
except Exception as e:
print(f"An error occurred: {e}")
If you wanted to keep away from YANG Models you can construct XML requests and filters without explicitly referencing YANG models. (check this example and update for your needs as required)
from ncclient import manager
from lxml import etree
# ... (same connection setup as before)
with manager.connect(...) as m:
# Construct the XML filter directly
filter_string = """
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-native">
<ip>
<route>
</route>
</ip>
</native>
</filter>
"""
response = m.get_config(source='running', filter=('subtree', filter_string))
# ... (parse and process the XML as before)
Take a look at https://developer.cisco.com/yangsuite/ this will really help you in your quest for learning netconf and xml with Cisco devices.
Hope this helps.
Connect with me https://bigevilbeard.github.io
