Following the link mentioned below was able to pull info from the config via API. Looking for how to pull info from the API that exist in show commands. That is to get the output of show cdp n or show ip route etc.
If you want access to the CDP or routing information there are models to retrieve that via netconf/Yang. This is called operational-data or oper-data. On a 3650 you need to run 16.5 code for this. This would give you structured data.
You need to select the right model/filter to use to make the request. For routing state you can do a netconf GET for <routing-state/>.
While NETCONF (and RESTCONF) do provide some operational data, it isn't an exact match to what you see in "show" commands. However, these protocols do provide for some operational data in a structured (i.e., JSON or XML) format. For example, our devices expose all SNMP MIB data via YANG data models, so they are accessible via NETCONF or RESTCONF. The Python script will dump the contents of the CDP neighbor table in JSON (via NETCONF). You could use this to then build the "show cdp neighbor" output.
{ "data": { "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0", "@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0", "CISCO-CDP-MIB": { "@xmlns": "urn:ietf:params:xml:ns:yang:smiv2:CISCO-CDP-MIB", "cdpCacheTable": { "cdpCacheEntry": [ { "cdpCacheIfIndex": "1", "cdpCacheDeviceIndex": "2", "cdpCacheAddressType": "ip", "cdpCacheAddress": "wKgBJA==", "cdpCacheVersion": "Cisco IOS Software, C3560 Software (C3560-IPSERVICESK9-M), Version 15.0(2)SE10, RELEASE SOFTWARE (fc2)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2016 by Cisco Systems, Inc.\nCompiled Mon 18-Jul-16 02:05 by prod_rel_team", "cdpCacheDeviceId": "jabberwocky.marcuscom.com", "cdpCacheDevicePort": "FastEthernet0/11", "cdpCachePlatform": "cisco WS-C3560V2-24TS", "cdpCacheCapabilities": "AAAAKQ==", "cdpCacheVTPMgmtDomain": "MARCUSCOM", "cdpCacheNativeVLAN": "1", "cdpCacheDuplex": "fullduplex", "cdpCacheLastChange": "1132114" }, { "cdpCacheIfIndex": "8", "cdpCacheDeviceIndex": "1", "cdpCacheAddressType": "ip", "cdpCacheAddress": "wKgBLg==", "cdpCacheVersion": "1", "cdpCacheDeviceId": "00180a974f01", "cdpCacheDevicePort": "Port 3", "cdpCachePlatform": "MS220-24P", "cdpCacheCapabilities": "AAAACA==", "cdpCacheVTPMgmtDomain": null, "cdpCacheNativeVLAN": "1", "cdpCacheDuplex": "unknown", "cdpCacheLastChange": "1082893" } ] } } } }
The same can be done with the routing table and the ietf-routing module:
{ "data": { "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0", "@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0", "routing": { "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-routing", "routing-instance": [ { "name": "Mgmt-vrf", "interfaces": { "interface": [ "Loopback0", "GigabitEthernet0/0" ] }, "routing-protocols": { "routing-protocol": { "type": "static", "name": "1", "static-routes": { "ipv4": { "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-ipv4-unicast-routing", "route": { "destination-prefix": "0.0.0.0/0", "next-hop": { "next-hop-address": "192.168.1.1" } } } } } } }, { "name": "default", "description": "default-vrf [read-only]", "interfaces": null, "routing-protocols": { "routing-protocol": { "type": "static", "name": "1" } } } ] } } }
Make sure you change the body of the filter to
<routing-state xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
When the filter was changed got the below message...
{
"data": {
"@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
"@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0"
}
}
Running 16.5.1a version of code on 3650
you can work out the filters from the yang model.
This is the filter statement you need in the python script
rtr_filter = '''
<filter>
<routing-state xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
</filter>
'''
The filters are derived from YANG modules. Currently, all of the Cisco YANG modules can be found at yang/vendor/cisco/xe at master · YangModels/yang · GitHub based on version of code. Check the blog write-up at Getting To Know YDK that walks through YANG and the YANG Development Kit. The first part of this talks about YANG itself. The pyang tool can be very helpful to show the various codes one can use as filters.
Also check a couple of intro blog posts too. Part2 talks about operational data.
If you can't get routing oper data from a 3650 ( CSR handy at the moment), try using the IP-FORWAR-MIB:
<IP-FORWARD-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:IP-FORWARD-MIB"/>.
works on 3850. maybe need to enable oper-data.
Want to know How to enable operational data
<IP-FORWAR-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:IP-FORWARD-MIB"/>
yields below.
{
"data": {
"@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
"@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0"
}
}
C:\Users\mgrous\Google Drive\Glastonbury Lab\Automation\3650>
To enable operational data..you need the following command:
conf t
netconf-yang cisco-odm polling-enable
It may be hitting the timeout. Change the manager line to:
with manager.connect_ssh(host=RTR_IP, port=830, username='admin', hostkey_verify=False, password=$$$$$$', timeout=300) as m:
When tried to add timeout 300 it just hangs there for 300 seconds...Then says this...
Failed to get CDP: ncclient timed out while waiting for an rpc reply.
Then if i hit cntrl c it says this...
<module>
print('Failed to get CDP: {}'.format(e))
File "C:\Python27\lib\site-packages\ncclient\manager.py", line 214, in __exit__
self.close_session()
File "C:\Python27\lib\site-packages\ncclient\manager.py", line 162, in wrapper
return self.execute(op_cls, *args, **kwds)
File "C:\Python27\lib\site-packages\ncclient\manager.py", line 232, in execute
raise_mode=self._raise_mode).request(*args, **kwds)
File "C:\Python27\lib\site-packages\ncclient\operations\session.py", line 28, in request
return self._request(new_ele("close-session"))
File "C:\Python27\lib\site-packages\ncclient\operations\rpc.py", line 322, in _request
self._event.wait(self._timeout)
File "C:\Python27\lib\threading.py", line 614, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
KeyboardInterrupt
If you turn on odm-polling you should get routing state information.
This is what mentioned in the script....
#!/usr/bin/env python
#
# Copyright (c) 2017 Joe Clarke <jclarke@cisco.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
from ncclient import manager
import xmltodict
import json
RTR_IP = "10.119.192.10"
rtr_filter = '''
<filter>
<IP-FORWARD-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:IP-FORWARD-MIB"/>
</filter>
'''
with manager.connect_ssh(host=RTR_IP, port=830, username='admin', hostkey_verify=False, password='CIMC123!@#') as m:
try:
c = m.get(rtr_filter).data_xml
d = xmltodict.parse(c)
print(json.dumps(d, indent=4))
except Exception as e:
print('Failed to get CDP: {}'.format(e))
you need to change the filter too.
Change:
rtr_filter = '''
<filter>
<IP-FORWARD-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:IP-FORWARD-MIB"/>
</filter>
'''
to
rtr_filter = '''
<filter>
<routing-state xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
</filter>
'''
output is ....
{
"data": {
"@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
"@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
"routing-state": {
"@xmlns": "urn:ietf:params:xml:ns:yang:ietf-routing",
"routing-instance": [
{
"name": "Mgmt-vrf",
"type": "vrf-routing-instance",
"ribs": {
"rib": [
{
"name": "ipv4-default",
"address-family": {
"@xmlns:v4ur": "urn:ietf:params:xml:ns:yang:ietf-ipv4-unicast-routing",
"#text": "v4ur:ipv4-unicast"
},
"default-rib": "true",
"routes": {
"route": [
{
"destination-prefix": "0.0.0.0/0",
"route-preference": "1",
"metric": "0",
"next-hop": {
"next-hop-address": "10.66.104.65"
},
"source-protocol": "static"
},
{
"destination-prefix": "10.66.104.111/32",
"route-preference": "0",
"metric": "0",
"next-hop": {
"outgoing-interface": "GigabitEthernet0/0"
},
"source-protocol": "direct"
<SNIP>
The "routing" filter will show you config data. Operational data can be found at "routing-state" branch. You can also get this from the IP-FOWARD-MIB module. Today, operational state and configuration are separated either into different modules or in difference sub-trees. There is an effort in the IETF to help revise this to make it easier to deal with configuration data overlaid with operational state.
Comments
0 comments
Please sign in to leave a comment.