cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
228
Views
1
Helpful
2
Replies

Parsing NETCONF RPC Reply (BGP Neighbors) to Python Dictionary

dudster83
Level 1
Level 1

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.

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

You may use xmltodict to parse the xml output into a dict:

https://pypi.org/project/xmltodict/

HTH

View solution in original post

2 Replies 2

Marcel Zehnder
Spotlight
Spotlight

You may use xmltodict to parse the xml output into a dict:

https://pypi.org/project/xmltodict/

HTH

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'}