> For the complete documentation index, see [llms.txt](https://ccie-sp.gitbook.io/ccie-spv5.1-labs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ccie-sp.gitbook.io/ccie-spv5.1-labs/labs/nso/nso-api-using-python-3.md).

# 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 <a href="#id-6e4a28d8-57e2-42e2-857c-49bd119c7b96" id="id-6e4a28d8-57e2-42e2-857c-49bd119c7b96"></a>

```
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:
```
