06-11-2020 11:49 AM
Hello,
How to display all configuration on a device using python maapi, like CLI:
show running-config devices device mydevice config
06-11-2020 02:53 PM
There is no super-packaged way of doing this, people are usually trying to navigate a model.
But there are a few options, perhaps the fastest and most straight-forward is to use maapi.save_config to get all of it in a good format. That call is not the easiest to figure out, here is an old example (see the read_config function) of how to put it together: https://github.com/NSO-developer/opa-example/blob/f46d16f4b40c92bc228bc8f61acac777731e88f6/packages/opa/python/opa/sub.py
06-12-2020 03:40 AM - edited 06-12-2020 03:42 AM
Thats seems not normal to do, does it affect NSO performance ?
So, I try to get parts of config. I can get description on device interface using python maapi as below:
import ncs.maapi as maapi
with ncs.maapi.single_read_trans('admin', 'system', db=ncs.OPERATIONAL) as m:
root = ncs.maagic.get_root(m)
Print(root.devices.device['mydevice'].config.ios__interface.GigabitEthernet['1'].description)
my_interface_desc
Instead of getting a single value, can I get the output of all configuration lines below the interface (e.g. description, ip address, negotiation, ...) using python maapi, something like CLI:
root.ncs__devices.device['mydevice'].config.ios__interface.GigabitEthernet['1'].?
Even if I don't know the leaves configured below the interface, may be the description or ip address is not configured, simply I need to get all lines below interface whatever they are.
06-12-2020 07:46 AM
It isn't a super expensive operation as such, but I would not recommend dumping a full device tree from inside a services, doing it on an interface could be okay.
There are other ways, in particular you can use a for loop on a maagic object to get it's children, can you explain _why_ you want this? What is it you are trying to achieve?
06-12-2020 07:57 AM - edited 06-12-2020 07:58 AM
some interfaces have xconnect, others have ip address, so I want to display the configuration below the interface, so that I can check if it has xconnect or ip address configured.
06-12-2020 08:43 AM
06-12-2020 10:14 AM
I am just giving an example, its more than this like policy, instance, negotiation, description and more, so I need the string representation for all config below the interface.
06-12-2020 10:48 AM
02-21-2022 11:34 AM
Hi,
I know this is 'necroing', however, this thread helped me greatly, so I will just put this here:
from ncs import maapi import _ncs import socket def recv_all_and_close(c_sock): data = '' while True: buf = c_sock.recv(4096) if buf: data += buf.decode('utf-8') else: c_sock.close() return data if __name__ == "__main__": style = (_ncs.maapi.CONFIG_JSON) with maapi.Maapi() as connection: with maapi.Session(connection, 'admin', 'test_context'): with connection.start_read_trans() as trans: c_id = _ncs.maapi.save_config(connection.msock, trans.th, style, '/devices') c_sock = socket.socket() (ncsip, ncsport) = connection.msock.getpeername() _ncs.stream_connect(c_sock, c_id, 0, ncsip, ncsport) data = recv_all_and_close(c_sock) print(data)
In case someone else lands here, just like I did
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