10-13-2020 08:13 PM - edited 10-13-2020 08:15 PM
Hi guys, I am new to cisco router and ydk.
I have a problem need your help.
env:
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.
10-14-2020 08:50 AM
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)
10-14-2020 09:42 AM
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.
10-15-2020 12:50 AM
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.
10-15-2020 04:18 AM - edited 10-15-2020 04:19 AM
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
10-15-2020 11:44 AM
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.
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