The "nc-read-xr-ifmgr-cfg-11-ydk.py", the process is just "pass"
"""Process data in interface_configurations object."""
Tried to modify a bit but got the response "None".
"""Process data in interface_configurations object."""
print ('interface name: ',int.interface_name)
I believe it should be a list of configuration... but don't know how it should be...
any sample can be reference.
All basic sample apps in the range of 10-19 are boilerplates (templates). Those boilerplates do no processing and provide no output. The convention should be described in the README:
ydk-py-samples/samples/basic at master · CiscoDevNet/ydk-py-samples · GitHub
Now, for a sample implementation of processing interface config, here's a very simple function to print the interface names:
def process_interface_configurations(interface_configurations): """Process data in interface_configurations object.""" for interface_configuration in interface_configurations.interface_configuration: print(interface_configuration.interface_name)
The entire configuration hierarchy is documented at:
The function above produces the following output when the script is run against one of my routers:
MgmtEth0/RP0/CPU0/0 GigabitEthernet0/0/0/0 GigabitEthernet0/0/0/1
- Is there a way to read all configurations under a specific interface.
Sure thing. Note that the object passed to crud.read really acts as a filter. So, you need to construct an object with an interface with the name you're interested in. Interface name is the key of the list of interfaces defined in the model:
# create read filter for mgmt interface interface_configurations = xr_ifmgr_cfg.InterfaceConfigurations() interface_configuration = interface_configurations.InterfaceConfiguration() interface_configuration.interface_name ="MgmtEth0/RP0/CPU0/0" interface_configurations.interface_configuration.append(interface_configuration) # read data from NETCONF device interface_configurations = crud.read(provider, interface_configurations)
Actually, you can be more clever and just instantiate the class that defines an interface. YDK will do the right thing and build the proper filter with the full hierarchy even though you are reading a more specific object:
# create read filter for mgmt interface interface_configuration = xr_ifmgr_cfg.InterfaceConfigurations.InterfaceConfiguration() interface_configuration.interface_name ="MgmtEth0/RP0/CPU0/0" # read data from NETCONF device interface_configuration = crud.read(provider, interface_configuration)
Let's say we just want to print the interface description once read:
Here's a sample output on a mgmt interface with an obvious description:
$ ./nc-read-xr-ifmgr-cfg-99-ydk.py ssh://admin:admin@router
*** MANAGEMENT INTERFACE ***
Here's the full app log:
$ ./nc-read-xr-ifmgr-cfg-99-ydk.py ssh://admin:admin@router -v
2016-12-13 14:44:10,148 - ydk.providers.netconf_provider - INFO - NetconfServiceProvider connected to router:None using ssh
2016-12-13 14:44:10,211 - ydk.services.crud_service - INFO - READ operation initiated
2016-12-13 14:44:10,213 - ydk.providers._provider_plugin - DEBUG -
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:63dbbab9-2cce-48e8-8437-832206be1989">
2016-12-13 14:44:10,277 - ydk.providers._provider_plugin - DEBUG -
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:63dbbab9-2cce-48e8-8437-832206be1989">
<description>*** MANAGEMENT INTERFACE ***</description>
2016-12-13 14:44:10,278 - ydk.services.crud_service - INFO - READ operation completed
*** MANAGEMENT INTERFACE ***
2016-12-13 14:44:10,500 - ydk.providers.netconf_provider - INFO - NetconfServiceProvider disconnected from router using ssh
- How do you print the whole XML interface in a single print statement.. Similar to what is in the rpc-reply data group.
The codec service decodes and encodes data. The ydk-py-samples repo has numerous sample apps using codec service.
- Tried but they are not working so well for me.
- tried the examples:
- Have uncommented the print statement in each, ran similar to: (like README example says to)
- cd-encode-xr-ifmgr-cfg-11-ydk.py ssh://jb:email@example.com
- And get:
- usage: cd-encode-xr-ifmgr-cfg-11-ydk.py [-h] [-v]
- cd-encode-xr-ifmgr-cfg-11-ydk.py: error: unrecognized arguments: ssh://jb:firstname.lastname@example.org
- Running the script, as is, gives me simply this output:
- <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"/>
- Also, if I'm looking for decode, I'm seeing no examples.
Codec executes local encoding/decoding:
./cd-encode-xr-ifmgr-cfg-11-ydk.py # <-- no need for credentials
if you read the description at https://github.com/CiscoDevNet/ydk-py-samples/tree/master/samples/basic you'll see that the basic samples in the 10-19 range are boilerplates (templates). They contain no configuration. For interfaces, there are samples from 20 through 36 with useful configuration. For instance:
ydk-py-samples/cd-encode-xr-ifmgr-cfg-36-ydk.py at master · CiscoDevNet/ydk-py-samples · GitHub
and it's XML output:
ydk-py-samples/cd-encode-xr-ifmgr-cfg-36-ydk.xml at master · CiscoDevNet/ydk-py-samples · GitHub
As the help string indicates, the codec sample apps take no argument to connect to a device. These apps only encode a Python object into XML and print the resulting string. Hope this clarifies.
- hoping to print a router's interface XML information via my own print command, not something local.
It would be like ...
1. set the provider variable with router info, username, password
2. set interface filter
3. then print like: print( <????>(provider, interface_configurations))
- this would print out the complete router's interface, or whatever I've may have filtered on... like a specific interface-name.
If you're looking at an XML-centric use case, it may be that an approach other than the YDK is what you're looking for. For example, you might want to use the ncclient library directly. You can see samples of how to use ncclient here:
You can see sample filters to use with the script ncc.py at:
These are just standard netconf subtree filters that can be used with get/get_config from ncclient, as shown in the script ncc.py.
You can get the interface by using the following or similar XML via netconf session:
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
That should give you an xml configuration which contains information about interfaces.
If you are using ncclient on python, you can make a variable containing the filter part of the xml (highlighted in bold above) and use it on ncclient
Example (assuming my_nc as ncclient instance that is already connected to your router)
result = my_nc.get_config("running", my_filter)
You can then parse result to get the info that you want. Python has a lot of module dealing with xml (minidom, elementree, xmltodict...)
For example, this one will give you an indented XML view of the result
report = xml.dom.minidom.parseString(result.xml)
Do not have IOS-XR. I uploaded my sample script at https://github.com/snakeskinsalamander/Netconf-example just in case. It is using an always-on XE on devnet sandbox though.
This is as simple as you could make a call to get interface information into an XML output using Python and the ncclient.
from ncclient import manager
from lxml import etree
if __name__ == '__main__':
omg = '<interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"/>'
m = manager.connect(host="my.router.com",
data = m.get_config("running",'<filter>'+omg+'</filter>')
print etree.tostring(etree.fromstring(data.xml), pretty_print=True)
took this right from the #10 example above.
Sharing with you bgp oper filter which is working . It takes just VRF names, networks and route targets from bgp instance. Using it as "subtree" filter with single tag when interesting to get some information from device. Do you happen to know if there is another syntax/functionality for the filtering. There is also xpath possibility but seems IOXR doesnt support xpath capability on 6.1.2 version.
filter = '''<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-bgp-oper">
When dealing with netconf/yang on IOS-XR, subtree filtering is the only query syntax supported. No timelines for support of XPath filtering at this time, and no other standardised alternative.
Please sign in to leave a comment.