NSO API using Python #3

Load blank.ssh.enabled.cfg

#IOS-XE
config replace flash:blank.ssh.enabled.cfg
 
#IOS-XR
configure
load bootflash:blank.ssh.enabled.cfg
commit replace
y

In this task, you will use a single maapi session with multiple transactions.

Create a script which iterates over the XE group and prints the hostname of each device Use a second transaction to print the gi2.254 address. Use a single session for both transactions.

Answer

import ncs

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, 'admin', 'python'):
        with m.start_read_trans() as t:
            root = ncs.maagic.get_root(t)
            device_group = root.devices.device_group['XE']
            for device in device_group.device_name:
                print(root.devices.device[device].config.ios__hostname)

        with m.start_read_trans() as t:
            root = ncs.maagic.get_root(t)
            device_group = root.devices.device_group['XE']
            for device in device_group.device_name:
                print(root.devices.device[device].config.ios__interface.GigabitEthernet['2.254'].ip.address.primary.address)

Explanation

Using the maapi Session(), you can create multiple transactions without restarting the session. First you create the Maapi object as m, and then reference this object with the AAA details (user and context):

import ncs

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, 'admin', 'python'):

Next, you use another with block per transaction. Use start_read_trans() and start_write_trans() instead of single_read_trans() and single_write_trans().

import ncs

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, 'admin', 'python'):
        with m.start_read_trans() as t:

        with m.start_read_trans() as t:

Last updated