12-07-2016 07:25 PM
How to retrieve the interface configuration??
I saw 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
I tried to modified a bit but I 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???
Thanks a lot!!
12-08-2016 09:16 AM
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
Hope that helps!
12-09-2016 01:37 AM
Million Thanks!! IT works like a charm! I now start understand the idea to learn ydk.
One more question, is there a way to read all configurations under a specific interface??
12-13-2016 02:50 PM
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
$
Hope that helps!
07-13-2017 12:38 PM
How do you print the whole XML interface in a single print statement? Similar to what is in the rpc-reply data group?
07-13-2017 12:54 PM
The codec service decodes and encodes data. The ydk-py-samples repo has numerous sample apps using codec service.
HTH.
07-13-2017 01:24 PM
Thank you for pointing me over to codec service.... which I tried but they are not working so well for me.
I've tried the examples:
cd-encode-xr-ifmgr-cfg-10-ydk.py
cd-encode-xr-ifmgr-cfg-11-ydk.py
I've 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?
07-13-2017 02:50 PM
Codec executes local encoding/decoding:
./cd-encode-xr-ifmgr-cfg-11-ydk.py # <-- no need for credentials
07-13-2017 04:13 PM
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.
07-14-2017 06:51 AM
It is helpful, but I was 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.
07-14-2017 07:33 AM
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.
Cheers,
Einar
07-14-2017 07:55 AM
Thank you einarnn and saalvare for your patience, responses and help! Einar, I will take a look at that. I'm not seeing what I need in the ydk, but I know what I'm looking for has to be possible.
The reason I figured there has to be a way to be able to print the entire interface is because every time I run one of the ifmgr tests, I see the whole interface print as standard out from some internal processing. I was guessing somewhere embedded in the, like, crud.read processing, it is outputting all of an interface' XML, it's pulling the whole thing... because I'm seeing it flash on my screen! It would be nice if that raw XML was a part of the structure (or dictionary? class? Sorry, I'm not a python expert.) that is returned from that crud.read.
Thanks,
James
07-15-2017 06:47 AM
I believe 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())
I hope that helps.
07-28-2017 12:58 AM
Hi Vincent. Do you happen to have any current working example using ncclient towards XR device with the filter like in your post? I am not able to find anything working on Internet. And the examples/documentation on the git/ncclient is not quite help for me I would appreciate anything as I am just getting just errors and errors.... Started being little bit frustrated
Thanks!
07-28-2017 03:03 AM
Hmmmmm I found Einars baby the "ncc" (very valuable resource for ncclient):
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