05-05-2024 11:21 AM
Hello
This is more a python question rather than a NETCONF/Yang question.
I'm getting data from an IOS-XR device using NETCONF about bgp peers in the device.
I need to parse the output i'm getting into a dictionary for further use in the code
The queried RPC is
from ncclient import manager
import configparser
config = configparser.ConfigParser()
config.read('/home/user/dev/workshop-configuration.ini')
device=config['edge100.local']
netconf_connection = manager.connect(host=device['Host'],
port=device['NCPort'],
username=device['UserName'],
password=device['Password'],
hostkey_verify=False,
device_params={'name':'iosxr'})
netconf_filter = """
<filter>
<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-bgp-oper">
<instances>
<instance>
<instance-active>
<vrfs>
<vrf>
<vrf-name>default</vrf-name>
<afs>
<af>
<af-name>ipv4-unicast</af-name>
<networks/>
</af>
</afs>
<neighbors>
<neighbor>
<neighbor-address>
</neighbor-address>
<remote-as>
</remote-as>
<connection-state>
</connection-state>
<description>
</description>
</neighbor>
</neighbors>
</vrf>
</vrfs>
</instance-active>
</instance>
</instances>
</bgp>
</filter>
"""
response = netconf_connection.get(netconf_filter)
print(response)
The results returns as string
<?xml version="1.0"?>
<rpc-reply message-id="urn:uuid:c489c7c7-5c12-42f1-abee-61d32e2ff494" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-bgp-oper">
<instances>
<instance>
<instance-name>default</instance-name>
<instance-active>
<vrfs>
<vrf>
<vrf-name>default</vrf-name>
<neighbors>
<neighbor>
<neighbor-address>100.100.100.140</neighbor-address>
<description>PEER to 140</description>
<remote-as>140</remote-as>
<connection-state>bgp-st-active</connection-state>
</neighbor>
<neighbor>
<neighbor-address>100.100.200.141</neighbor-address>
<description>PEER To 141</description>
<remote-as>141</remote-as>
<connection-state>bgp-st-active</connection-state>
</neighbor>
</neighbors>
</vrf>
</vrfs>
</instance-active>
</instance>
</instances>
</bgp>
</data>
</rpc-reply>
The desired procedure output is a dictionary where neighbor is a key for address,description,remote-as, status
Will appreciate any kind of assistance with this procedure.
Solved! Go to Solution.
05-06-2024 01:14 AM
You may use xmltodict to parse the xml output into a dict:
05-06-2024 01:14 AM
05-07-2024 09:25 AM
Hi,
Thanks, xmltodict did the job
import xmltodict
xdict = xmltodict.parse(netconf_answer)
xdict2=(xdict["rpc-reply"]["data"]["bgp"]["instances"]["instance"]["instance-active"]["vrfs"]["vrf"]["neighbors"]["neighbor"])
for i in xdict2:
print(i)
output
{'neighbor-address': '1.11.12.140', 'description': 'PEER to 140', 'remote-as': '140', 'connection-state': 'bgp-st-active'}
{'neighbor-address': '1.11.12.141', 'description': 'PEER To 141', 'remote-as': '141', 'connection-state': 'bgp-st-active'}
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide