cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
815
Views
5
Helpful
5
Replies

Get stuck when running ydk-py hello world sample

ElvisLou15831
Level 1
Level 1

Hi guys, I am new to cisco router and ydk.

I have a problem need your help.

 

env: 

IOS XRv 9000 virtual Router

 

ydk-code:

from ydk.services import CRUDService
        from ydk.providers import NetconfServiceProvider
        from ydk.models.cisco_ios_xr import Cisco_IOS_XR_shellutil_oper \
            as xr_shellutil_oper
        from datetime import timedelta

        if __name__ == "__main__":
            """Main execution path"""

            # create NETCONF session
            provider = NetconfServiceProvider(address="10.0.0.1",
                                              port=830,
                                              username="admin",
                                              password="admin",
                                              protocol="ssh")
            # create CRUD service
            crud = CRUDService()

            # create system time object
            system_time = xr_shellutil_oper.SystemTime()

            # read system time from device
            system_time = crud.read(provider, system_time)

            # Print system time
            print("System uptime is " +
                  str(timedelta(seconds=system_time.uptime.uptime)))

            # close NETCONFIG session and exit
            provider.close()
            exit()

What I observed is that the code is stucking at the line "system_time = crud.read(provider, system_time)"

no error printed in log,

what can i do now? thanks. please point out if need more context.





5 Replies 5

MahdiR
Level 1
Level 1

Try adding this to your code. It will give you information about the problem.

import logging

log_ydk = logging.getLogger('ydk')
log_ydk.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
log_ydk.addHandler(handler)

yangorelik
Spotlight
Spotlight

Also check if Netconf server is configured on your virtual router and you can run CLI to it:

ssh admin@10.0.0.1 -p 830 -s netconf

 Afer  entering password you are expected to get Netconf server capabilities.

Yan Gorelik
YDK Solutions

Hi yangorelik,thanks for the reply.

Now I can use the YDK api to read the system time from the router and call other api to do some operation.

 

but i am not sure if the operation is successful or not ?

is there anywhere in the router can have the log which i can check?

 

Thanks very much.

Good day ElvisLou,

Not sure if I fully understand what you mean, but the logging I have in my code is done this way:

log = logging.getLogger('ydk')
log.setLevel(level)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)

Hope that was to some help.

Kind regards,
Simon Johansen

Hi Elvis

To check if operation was successful you need to use YDK API. For example (see CRUDService API

            # read system time from device
            system_time = crud.read(provider, system_time)
            if system_time:
print("System uptime is " + str(timedelta(seconds=system_time.uptime.uptime)))
else:
print("Failed to read system time")

You also can catch runtime exceptions:

        # read system time from device
try: system_time = crud.read(provider, system_time) if system_time:
print("System uptime is " + str(timedelta(seconds=system_time.uptime.uptime)))
else:
print("Failed to read system time")
except YError as err:
print("Exception occurred during crud.read operation: %s" % err.message)

In general it is good programming practice to handle in the application all available runtime errors.

Yan Gorelik
YDK Solutions