cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1529
Views
5
Helpful
4
Replies

Get the value of IP address on device interface using python maapi

nso20
Level 1
Level 1

Hello,

 

I can get the value of description on device interface using python maapi as below, but I couldnot get the value of ip address:

import ncs
import ncs.maagic as maagic
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

    print(root.devices.device['mydevice'].config.ios__interface.GigabitEthernet[1].ip.address)

the value returned is the word address not the ip address itself.

 

device: cisco IOS XE

NSO: 4.7.5

 

1 Accepted Solution

Accepted Solutions

lmanor
Cisco Employee
Cisco Employee

Right, you need to look at how the interface address is modeled:

 

This is what I see on 5.3.2.1 with IOS ned v 6.46:

admin@ncs% show devices device ios-0 config interface GigabitEthernet 0/0 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <devices xmlns="http://tail-f.com/ns/ncs">

    <device>

      <name>ios-0</name>

      <config>

        <interface xmlns="urn:ios">

          <GigabitEthernet>

            <name>0/0</name>

            <description>Test interface</description>

            <ip>

              <address>

                <primary>

                  <address>10.0.1.0</address>

                  <mask>255.255.255.0</mask>

                </primary>

              </address>

            </ip>

          </GigabitEthernet>

        </interface>

      </config>

    </device>

  </devices>

</config>

 

So you'll need to add in 'ip.address.primary.address' to get address:

 

In [12]: with ncs.maapi.Maapi() as m:

    ...:   with ncs.maapi.Session(m, 'admin', 'system'):

    ...:     with m.start_write_trans() as t:

    ...:         root = ncs.maagic.get_root(t)

    ...:         dev = root.ncs__devices.device['ios-0']

    ...:         devname = dev.name

    ...:         print (devname)

    ...:         desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].description

    ...:         print (desc)

    ...:         addr = desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].ip.address.primary.address

    ...:         print (addr)

    ...:         mask = desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].ip.address.primary.mask

    ...:         print (mask)

    ...:

ios-0

Test interface

10.0.1.0

255.255.255.0

 

 

View solution in original post

4 Replies 4

lmanor
Cisco Employee
Cisco Employee

Right, you need to look at how the interface address is modeled:

 

This is what I see on 5.3.2.1 with IOS ned v 6.46:

admin@ncs% show devices device ios-0 config interface GigabitEthernet 0/0 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <devices xmlns="http://tail-f.com/ns/ncs">

    <device>

      <name>ios-0</name>

      <config>

        <interface xmlns="urn:ios">

          <GigabitEthernet>

            <name>0/0</name>

            <description>Test interface</description>

            <ip>

              <address>

                <primary>

                  <address>10.0.1.0</address>

                  <mask>255.255.255.0</mask>

                </primary>

              </address>

            </ip>

          </GigabitEthernet>

        </interface>

      </config>

    </device>

  </devices>

</config>

 

So you'll need to add in 'ip.address.primary.address' to get address:

 

In [12]: with ncs.maapi.Maapi() as m:

    ...:   with ncs.maapi.Session(m, 'admin', 'system'):

    ...:     with m.start_write_trans() as t:

    ...:         root = ncs.maagic.get_root(t)

    ...:         dev = root.ncs__devices.device['ios-0']

    ...:         devname = dev.name

    ...:         print (devname)

    ...:         desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].description

    ...:         print (desc)

    ...:         addr = desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].ip.address.primary.address

    ...:         print (addr)

    ...:         mask = desc = root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].ip.address.primary.mask

    ...:         print (mask)

    ...:

ios-0

Test interface

10.0.1.0

255.255.255.0

 

 

Thanks a lot, but why you use write_trans() instead of read_trans(), although we are reading values from CDB ?

lmanor
Cisco Employee
Cisco Employee
Sorry for the confusion on read/write (which is really read-write) transaction, I just grabbed an example that I had handy that likely wrote to the CDB as well as read.
The start_read_trans() should work equally as well if you are just reading from the CDB.

@lmanor 

Many thanks for your quick response, last question, instead of getting a single value, can I get the output of whole configuration below the interface (e.g. description, ip address, ...) using python, something like:
root.ncs__devices.device['ios-0'].config.ios__interface.GigabitEthernet['0/0'].?