cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2195
Views
8
Helpful
8
Replies

Display all configuration on device using python maapi

nso20
Level 1
Level 1

Hello,

 

How to display all configuration on a device using python maapi, like CLI:

show running-config devices device mydevice config

8 Replies 8

vleijon
Cisco Employee
Cisco Employee

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

@vleijon 

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.

vleijon
Cisco Employee
Cisco Employee

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?

@vleijon 

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.

vleijon
Cisco Employee
Cisco Employee
If that is all you want, you can just check in the data tree which one is configured, I don’t think there is a big advantage of going via a string representation.

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.

vleijon
Cisco Employee
Cisco Employee
That is not the generally recommended way of doing it, we recommend that instead of exporting it as a string and parsing it yourself, you use the structured data provided by the API.

But, I understand that there might sometimes be reasons such as pre-existing code. In that case you can use the save_config method outlined above. If you use it only on the interface and not on the entire device config the performance impact should not be too bad, please check it out and see if it works for you.

u.avsec
Spotlight
Spotlight

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