cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1275
Views
2
Helpful
4
Replies

Unable to get show bgp summary output using YDK

Hi ,

Manually we able to get show bgp summary output using CLI .

Unable to get the same show bgp summary output using YDK

script details:

from ydk.services import CRUDService,CodecService

from ydk.providers import NetconfServiceProvider,CodecServiceProvider

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_shellutil_oper \

as xr_shellutil_oper

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes

from datetime import timedelta

provider = NetconfServiceProvider(address="192.168.10.10",

                                  username="username1",

                                  password="password1",

                                  protocol="ssh")

crud = CRUDService()

instance = Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances.Instance()

instance.instance_name = 'default'

vrf = Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances.Instance.InstanceActive.Vrfs.Vrf()

vrf.vrf_name = 'default'

neighbor =Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances.Instance.InstanceStandby.Vrfs.Vrf.Neighbors.Neighbor()

vrf.neighbors.neighbor.append(neighbor)

instance.instance_active.vrfs.vrf.append(vrf)

bgp.instances.instance.append(instance)

bgp_networks = crud.read(provider, neighbor,only_config=False)

print dir(bgp_networks)

print int(bgp_networks.neighbor_address)

print str(bgp_networks.connection_established_time)

print int(bgp_networks.connection_established_time)

provider.close()

Error output

-----------------

fabric@FabricAutomation:~/jay$ python bgp_neighbor1.py

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Traceback (most recent call last):

  File "bgp_neighbor1.py", line 42, in <module>

    print int(bgp_networks.neighbor_address)

AttributeError: 'NoneType' object has no attribute 'neighbor_address'

fabric@FabricAutomation:~/jay$

kindly let us know solution to findout the output for show bgp summary using YDK.

Thanks

Jay

4 Replies 4

einarnn
Cisco Employee
Cisco Employee

Jay,

Remember that the XR Oper data model for BGP and VRF content model the default VRF in a separate subtree. Here is a simple example that:

  • Queries for the default BGP instance

  • Iterates over the result:

  • For each instance (just the one called ‘default’):

  • Get a reference to the default VRF

  • Iterate over all neighbors, and for each neighbor:

  • Print the neighbor address

  • Print the remote AS

In my config, all I have is:

RP/0/RP0/CPU0:ios#show run router bgp

Tue Apr 18 07:31:26.138 UTC

router bgp 666

address-family ipv4 unicast

network 192.168.0.0/16

!

neighbor 192.168.1.1

remote-as 1

!

!

Hopefully the code below will be enough to get you started:

from ydk.services import CRUDService,CodecService

from ydk.providers import NetconfServiceProvider,CodecServiceProvider

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_shellutil_oper as xr_shellutil_oper

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes

from datetime import timedelta

from ydk.types import READ

provider = NetconfServiceProvider(address="127.0.0.1",

port=8300,

username="cisco",

password="cisco",

protocol="ssh")

crud = CRUDService()

q_instance = Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances.Instance()

q_instance.instance_name = 'default'

q_instances = Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances()

q_instances.instance.append(q_instance)

instances = crud.read(provider, q_instances)

for inst in instances.instance:

print('Instance: %s' % inst.instance_name)

default_vrf = inst.instance_active.default_vrf

for n in default_vrf.neighbors.neighbor:

print(' neighbor %s' % n.neighbor_address)

print(' remote-as: %s' % n.remote_as)

provider.close()

Cheers,

Einar

HI ,

I copied your script and executed in my setup .

got the below output :

fabric@FabricAutomation:~/jay$ sudo python bgp_neigh.py

Instance: default

[]

fabric@FabricAutomation:~/jay$

Getting empty list

However we got output using nccclient .

<data>

  <bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-bgp-oper">

   <instances>

    <instance>

     <instance-name>default</instance-name>

     <instance-active>

      <default-vrf>

       <neighbors>

        <neighbor>

         <neighbor-address>10.10.10.1</neighbor-address>

        </neighbor>

        <neighbor>

         <neighbor-address>10.10.10.2</neighbor-address>

        </neighbor>

       </neighbors>

      </default-vrf>

     </instance-active>

    </instance>

   </instances>

  </bgp>

</data>

</rpc-reply>

kindly do the needful for resolution

Jay,

I'm sorry, but there is not much more I can suggest at this point. The code as I suggested above works find on my test instance of xrv9k. You can try enabling debugs. For example:

import logging

LOGGING_TO_ENABLE = [

    'ncclient.transport.session',

    'ncclient.operations.rpc',

    'ydk.providers',

]

handler = logging.StreamHandler()

for l in LOGGING_TO_ENABLE:

    logger = logging.getLogger(l)

    logger.addHandler(handler)

    logger.setLevel(logging.DEBUG)

This will enable ncclient debugs and some YDK debug and may let you see what problems there may be.

Cheers,

Einar

saalvare
Cisco Employee
Cisco Employee

Here's a streamlined version assuming that you only care about default instance and global routing space.  Script output:

$ ./nc-read-xr-ipv4-bgp-oper-99-ydk.py ssh://xx:xx@host

Neighbor: 172.16.255.2

Established time: 224

$

Script:

#!/usr/bin/env python2

#

# Copyright 2016 Cisco Systems, Inc.

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#     http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#

"""

Read default VRF for model Cisco-IOS-XR-ipv4-bgp-oper.

usage: nc-read-xr-ipv4-bgp-oper-99-ydk.py [-h] [-v] device

positional arguments:

  device         NETCONF device (ssh://user:password@host:port)

optional arguments:

  -h, --help     show this help message and exit

  -v, --verbose  print debugging messages

"""

from argparse import ArgumentParser

from urlparse import urlparse

from ydk.services import CRUDService

from ydk.providers import NetconfServiceProvider

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper

import logging

if __name__ == "__main__":

    """Execute main program."""

    parser = ArgumentParser()

    parser.add_argument("-v", "--verbose", help="print debugging messages",

                        action="store_true")

    parser.add_argument("device",

                        help="NETCONF device (ssh://user:password@host:port)")

    args = parser.parse_args()

    device = urlparse(args.device)

    # log debug messages if verbose argument specified

    if args.verbose:

        logger = logging.getLogger("ydk")

        logger.setLevel(logging.DEBUG)

        handler = logging.StreamHandler()

        formatter = logging.Formatter(("%(asctime)s - %(name)s - "

                                      "%(levelname)s - %(message)s"))

        handler.setFormatter(formatter)

        logger.addHandler(handler)

    # create NETCONF provider

    provider = NetconfServiceProvider(address=device.hostname,

                                      port=device.port,

                                      username=device.username,

                                      password=device.password,

                                      protocol=device.scheme)

    # create CRUD service

    crud = CRUDService()

    instance = Cisco_IOS_XR_ipv4_bgp_oper.Bgp.Instances.Instance()  # create object

    instance.instance_name = 'default'

    default_vrf = crud.read(provider, instance.instance_active.default_vrf)

    for neighbor in default_vrf.neighbors.neighbor:

      print "Neighbor: {}".format(neighbor.neighbor_address)

      print "Established time: {}".format(neighbor.connection_established_time)

    provider.close()

    exit()

# End of script

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: