The "nc-read-xr-ifmgr-cfg-11-ydk.py", the process is just "pass"
def process_interface_configurations(interface_configurations):
"""Process data in interface_configurations object."""
pass
Tried to modify a bit but got the response "None".
def process_interface_configurations(interface_configurations):
"""Process data in interface_configurations object."""
int=interface_configurations.InterfaceConfiguration()
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:
http://ydk.cisco.com/py/docs/gen_doc_6bcc931a236e23d2877d3e742c4a01f5adc17782.html
The function above produces the following output when the script is run against one of my routers:
Loopback0
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:
print(interface_configuration.description)
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">
<get>
<filter type="subtree">
<interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg">
<interface-configuration>
<interface-name>MgmtEth0/RP0/CPU0/0</interface-name>
</interface-configuration>
</interface-configurations>
</filter>
</get>
</rpc>
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">
<data>
<interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg">
<interface-configuration>
<active>act</active>
<interface-name>MgmtEth0/RP0/CPU0/0</interface-name>
<description>*** MANAGEMENT INTERFACE ***</description>
<ipv4-network xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-io-cfg">
<addresses>
<primary>
<address>10.30.110.82</address>
<netmask>255.255.254.0</netmask>
</primary>
</addresses>
</ipv4-network>
</interface-configuration>
</interface-configurations>
</data>
</rpc-reply>
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:
- cd-encode-xr-ifmgr-cfg-10-ydk.py
- cd-encode-xr-ifmgr-cfg-11-ydk.py
- 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:password@abc.123
- 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:password@abc.123
- 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:
https://github.com/CiscoDevNet/ncc
You can see sample filters to use with the script ncc.py at:
https://github.com/CiscoDevNet/ncc/tree/master/snippets/filters
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">
<get-config>
<source><running/></source>
<filter>
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface></interface>
</interfaces>
</filter>
</get-config>
</rpc>]]>]]>
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)
my_filter= """
<filter>
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface></interface>
</interfaces>
</filter>
"""
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)
print(report.toprettyxml())
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.
#!/usr/bin/env python
import sys
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",
port="830",
username="me",
password="password",
device_params={'name':"csr"})
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">
<instances>
<instance>
<instance-name>default</instance-name>
<instance-active>
<vrfs>
<vrf>
<vrf-name/>
<afs>
<af>
<af-name>ipv4-unicast</af-name>
<networks>
<network>
<network/>
<local-process-instance-path>
<attributes-after-policy-in>
<common-attributes>
<extended-community/>
</common-attributes>
</attributes-after-policy-in>
</local-process-instance-path>
</network>
</networks>
</af>
</afs>
</vrf>
</vrfs>
</instance-active>
</instance>
</instances>
</bgp>
'''
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.
Comments
0 comments
Please sign in to leave a comment.