12-27-2020 08:49 PM
I am playing with the Netconf. I have tried both copying/pasting the XML code and with the python ncclient library. Here is my code for getting the entire router config.
--- XML ---
<?xml version="1.0" encoding="UTF-8" ?> <rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <get-config> <source> <running/> </source> </get-config> </rpc> ]]>]]>
--- python ncclient ---
from ncclient import manager iosxe_manager = manager.connect(...) iosxe_config = iosxe_manager.get_config('running')
In the result, I see the Gi1 interface but there is no IP associated with it. It says "DHCP" in it. Any idea why?
<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:7a39cf1f-09d9-45ac-8a6c-58c389fa8088"> <data> <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native"> ... <interface> <GigabitEthernet> <name>1</name> <ip> <address> <dhcp/> </address> <nat xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-nat"> <outside/> </nat> </ip> <mop> <enabled>false</enabled> <sysid>false</sysid> </mop> <negotiation xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ethernet"> <auto>true</auto> </negotiation> </GigabitEthernet> ... </interface> ... </native> ... </data> </rpc-reply>
I have tried with both 17.2 and 16.12 IOS on the CSR1000v and I am getting the same result..
BTW, is there recommended reading about how to come up with the XML based on the Yang model file, other than reading the RFC? I am curious to know how people comes out with the XML file.
Thanks!
Difan
Solved! Go to Solution.
12-28-2020 02:33 PM - edited 12-28-2020 02:35 PM
Hi Difan,
I'm a beginner myself, but I think what you get is normal and expected.
There are different APIs to work with network devices. NETCONF is used to read/write configuration. If you use NETCONF <get-config> to read the configuration, it is the equivalent of router command "show running-config", see below:
interface GigabitEthernet1 ip address dhcp negotiation auto no mop enabled no mop sysid end
What you see above is what NETCONF gets.
If your intent is to retrieve the dynamic IP assigned to an interface, you should use REST API instead of NETCONF. https://www.cisco.com/c/en/us/td/docs/routers/csr1000/software/restapi/restapi/RESTAPIipinterface.html
Hope this helps!
12-27-2020 11:04 PM
Hello Difan
You use YDK to serialize data model to XML and build RPC, then send it to device using NetconfServiceProvider. When getting response from device you deserialize XML payload back to data model.
12-28-2020 08:23 PM
Hi Yan, thanks for the information. I will do some research about the YDK. I am still wondering how you write the XML based on the Yang model file. Could you point me to some instructions? Thanks.
12-29-2020 03:52 PM
12-28-2020 02:33 PM - edited 12-28-2020 02:35 PM
Hi Difan,
I'm a beginner myself, but I think what you get is normal and expected.
There are different APIs to work with network devices. NETCONF is used to read/write configuration. If you use NETCONF <get-config> to read the configuration, it is the equivalent of router command "show running-config", see below:
interface GigabitEthernet1 ip address dhcp negotiation auto no mop enabled no mop sysid end
What you see above is what NETCONF gets.
If your intent is to retrieve the dynamic IP assigned to an interface, you should use REST API instead of NETCONF. https://www.cisco.com/c/en/us/td/docs/routers/csr1000/software/restapi/restapi/RESTAPIipinterface.html
Hope this helps!
12-30-2020 06:40 AM
It looks like NETCONF can also get the dynamic IP assigned to an interface. We just need to use a different YANG model. I tried "Cisco-IOS-XE-interfaces-oper.yang" and it worked. Not sure if there are any standard YANGs that can do the same. Here's the code from YANG Explorer:
import lxml.etree as ET from argparse import ArgumentParser from ncclient import manager from ncclient.operations import RPCError payload = """ <filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper"> <interface> <ipv4/> <ipv4-subnet-mask/> </interface> </interfaces> </filter> """ if __name__ == '__main__': parser = ArgumentParser(description='Usage:') # script arguments parser.add_argument('-a', '--host', type=str, required=True, help="Device IP address or Hostname") parser.add_argument('-u', '--username', type=str, required=True, help="Device Username (netconf agent username)") parser.add_argument('-p', '--password', type=str, required=True, help="Device Password (netconf agent password)") parser.add_argument('--port', type=int, default=830, help="Netconf agent port") args = parser.parse_args() # connect to netconf agent with manager.connect(host=args.host, port=args.port, username=args.username, password=args.password, timeout=90, hostkey_verify=False, device_params={'name': 'csr'}) as m: # execute netconf operation try: response = m.get(payload).xml data = ET.fromstring(response) except RPCError as e: data = e._raw # beautify output print(ET.tostring(data, pretty_print=True))
Here's the output:
<rpc-reply message-id="urn:uuid:cc48e512-deb2-41dc-9fa5-deb8e5d41276" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> <data> <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper"> <interface> <ipv4>192.168.28.132</ipv4> <ipv4-subnet-mask>255.255.255.0</ipv4-subnet-mask> </interface> </interfaces> </data> </rpc-reply>
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