08-18-2017 05:59 AM - edited 03-01-2019 03:57 AM
Hi team,
I'm trying to write a script in Python (2.7.10) to print the list of all devices configured in NSO (4.4.2), but couldn't find an example to get me started. I'm running into a (probably simple) Python issue ...
The root.devices.device is of type ncs.maagic.List. So, I tried to iterate through the list to get the name of each device :
import ncs
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session (m, 'admin', 'admin'):
root = ncs.maagic.get_root(m)
for dev in root.devices.device:
print type(dev) # print dev.name gives the same error ...
I get the following error message :
Traceback (most recent call last):
File "ncs_test.py", line 6, in <module>
for dev in root.devices.device:
File "/Users/mdeprete/nso/4.4.2/src/ncs/pyapi/ncs/maagic.py", line 1047, in __iter__
return ListIterator(self)
File "/Users/mdeprete/nso/4.4.2/src/ncs/pyapi/ncs/maagic.py", line 1229, in __init__
self._cursor = l._backend._cursor(l._path, l._key_enum_cs_nodes())
TypeError: _cursor() takes exactly 2 arguments (3 given)
Exception AttributeError: "'ListIterator' object has no attribute '_cursor'" in <bound method ListIterator.__del__ of <ncs.maagic.ListIterator object at 0x10c4e0c50>> ignored
I tried other List objects, and get the same error (like the resource pools for instance). If I use device['ios0'] for instance, then I can get access to all object data (config, address,...). So at least connectivity to my NSO is fine ...
I've looked at the manual, but there's no clear example on how to get all names of such a list. Is there any example out there I could use ?
Cheers,
Marc
Solved! Go to Solution.
08-18-2017 07:18 AM
The only thing missing here to make this work is a transaction object:
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session (m, 'admin', 'admin'):
with m.start_write_trans(ncs.RUNNING) as t:
root = ncs.maagic.get_root(t)
for dev in root.devices.device:
print dev.name
print type(dev) # print dev.name gives the same error ...
And the system was trying to tell you "ncs.maagic.BackendError: Not a Transaction backend", but due to a bug you got that other (highly cryptic) message instead.
Thanks for finding and reporting this!
08-18-2017 07:18 AM
The only thing missing here to make this work is a transaction object:
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session (m, 'admin', 'admin'):
with m.start_write_trans(ncs.RUNNING) as t:
root = ncs.maagic.get_root(t)
for dev in root.devices.device:
print dev.name
print type(dev) # print dev.name gives the same error ...
And the system was trying to tell you "ncs.maagic.BackendError: Not a Transaction backend", but due to a bug you got that other (highly cryptic) message instead.
Thanks for finding and reporting this!
08-18-2017 07:29 AM
And that was it, just one line, and it works perfectly now
Thanks Jan !
Cheers,
Marc
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