I need to fetch oper data from a device via netconfd using low-level cdb apis. However, when I run the below code via cdb.get(), it throws “Got exception badly formatted or nonexistent path (8): bad path” error. Am I doing wrong in passing the xpath?
The path is indeed invalid. There are only few ConfD API functions that work with proper XPath expressions, and none of them in the CDB API, the rest use keypaths. Have a look at the section “Using keypaths” in the CDB chapter of the ConfD user guide; in short, you always use syntax like
/path/to/a/list{keyval1 keval2}/subcontainer/leaf
For CDB API you can also use list[n-1] for the n-th instance of the list list. So there are two problems in your keypath: 1. {%s} should immediately follow device, without slash; 2. there are no predicates, if key is the only list key, just write ex4{%d}.
I think that means that either ex1 is not unique and you need to use a prefix like .../live-status/modprefix:ex1/..., or your device model has not been properly loaded by the system.
You might make things a bit easier for you if you use so-called high-level Python API and maagic API; I think you are using NSO - in that case check the corresponding chapters in NSO Development guide.
If things are set up correctly, something like the following should return True:
from ncs import maapi
with maapi.single_read_trans('admin', 'system') as tp:
root = maagic.get_root(tp)
device = root.devices.device['netconfd']
'ex1' in [c._name for c in device.live_status._children.get_children()]
(Note that the code is using attributes _children or _name that should be used only for debugging.)