cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1546
Views
30
Helpful
5
Replies

Netconf to retrieve ospf configuration using Python

sqambera
Level 1
Level 1

Hello,

I wrote following code to fetch ospf configuration from the CSR router but not able to obtain it. Please see the output. Can anyone please help me know what's wrong?

 

from xml.dom import minidom
from ncclient import manager

m = manager.connect(host = "10.10.20.48",
port = 830,
username = "developer",
password = "C1sco12345",
hostkey_verify = False)

print(m.connected)


filter_routing = """
<filter>
<ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf">
</ospf>
</filter>
"""
running = m.get_config("running", filter_routing)
running_xml = minidom.parseString(running.xml)
print(running_xml.toprettyxml(indent = " "))


OUTPUT:

C:\Users\Image\PycharmProjects\Netconf\venv\Scripts\python.exe C:/Users/Image/PycharmProjects/Netconf/Routing.py
True
<?xml version="1.0" ?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:902cbaa2-aea3-4e00-b19a-2278007b50f0">
<data/>
</rpc-reply>


The actual ospf configuration on router is following:

csr1000v-1#sh run | section ospf
router ospf 1
network 172.16.24.0 0.0.0.255 area 0


Attached is the XML output of running configuration.

Thanks,
Qamber



1 Accepted Solution

Accepted Solutions

You could also use the native model

 

        rpc = '''
                <config>
                    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
                        <router>
                            <ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf">
                            </ospf>
                        </router>
                    </native>
                </config>
            '''
Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

5 Replies 5

Alex Stevenson
Cisco Employee
Cisco Employee

 

Hello @sqambera,

 

You are on the right track! Cisco Code Exchange will help you complete the task. I searched there for “ospf, yang, netconf”:

 

Cisco Code Exchange

 

Examples found therein:

 

 

Of course, you will need to adjust your code, including filtering results for ospf.

You could also use the native model

 

        rpc = '''
                <config>
                    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
                        <router>
                            <ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf">
                            </ospf>
                        </router>
                    </native>
                </config>
            '''
Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hello @sqambera,

 

I'm running IOS-XR, so things may be different, but I was able to run the following command on the router to determine the available OSPF namespaces

 

show netconf-yang capabilities | in ospf

 

show netconf-yang capabilities | in ospf
Mon Dec  6 19:25:57.698 UTC
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-ospf-act                               |2016-09-14| 
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-ospf-cfg                               |2018-05-14| 
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-ospf-oper                              |2018-04-18| 
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv6-ospfv3-act                             |2016-09-14| 
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv6-ospfv3-cfg                             |2018-05-14| 
http://cisco.com/ns/yang/Cisco-IOS-XR-ipv6-ospfv3-oper                            |2018-06-15|

After a bit of trial and error, I found that I could produce useful output with the following filter:

filter_routing = """
<filter>            
<ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-ospf-cfg">
</ospf>  
</filter>
"""

The following is a portion of the output:

>>> print(running_xml.toprettyxml(indent = "  "))
<?xml version="1.0" ?>
<rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:f7d22dbd-db09-43b1-b0e9-c43909a359f4">
  
 
  <data>
    
  
    <ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-ospf-cfg">
      
   
      <processes>
        
    
        <process>
          
     
          <process-name>10</process-name>
          
     
          <nsr>true</nsr>
          
     
          <default-vrf>
            
      
            <adjacency-changes>detail</adjacency-changes>

 

Hope this helps!!!

 

fjm

@ittybittypacket

fracjackmac
Level 1
Level 1

Hello@sqambera,

 

By the way, THANKS for your post!!

 

This gave me an opportunity to actually take the leap and start exploring netconf in the lab.  

 

fjm

@ittybittypacket

Thank you