cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
17671
Views
16
Helpful
23
Replies

Cisco 3650 Show commands via API

mikegrous
Level 3
Level 3

I followed the below link and life is good on pulling info from the config via API. But how would I go about pulling info from the API that exist in show commands? IE get the output of show cdp n or show ip route etc?

Getting Started with NETCONF/YANG – Part 1

23 Replies 23

That made it worse...

-----------

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=$$$$$$') 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))

----------

Failed to get CDP: ncclient timed out while waiting for an rpc reply.

Traceback (most recent call last):

  File "nc-routing.py", line 47, in <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 343, in _request

    raise TimeoutExpiredError('ncclient timed out while waiting for an rpc reply.')

ncclient.operations.errors.TimeoutExpiredError: ncclient timed out while waiting for an rpc reply.

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 i 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

Yes,

I saw the same thing.

If you turn on odm-polling as in my earlier post you should get routing state information.

Adam

Sent from my iPhone

Unfortunately same thing..

3650>py nc-routing.py

Failed to get CDP: ncclient timed out while waiting for an rpc reply.

Traceback (most recent call last):

  File "nc-routing.py", line 46, in <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

This is what i have for a 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))

Yes, 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>

'''


I get lots of output....

{

    "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>    

Perfect that worked. Thank you much.

As Adam mentioned, 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.