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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ccie-sp.gitbook.io/ccie-spv5.1-labs/labs/nso/nso-api-using-python-3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
