cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1869
Views
10
Helpful
3
Replies

list/fetch all the existing interfaces name/id per device from CDB

mohamels
Cisco Employee
Cisco Employee

Hi , 

i'm trying to list all the existing interfaces per device from CDB using Python API to be used further to get more info per interface .

i got the interfaces without interfaces ID as shown below  !  what should i add as a variable to list the interfaces name/id ?

-

                Gig_Ints = device.config.ios__interface.GigabitEthernet
                vlan_Ints = device.config.ios__interface.Vlan
                print(Gig_Ints)
                print(vlan_Ints)

---output ---

GigabitEthernet
Vlan

 

i tried to follow the configuration path like [name] or [ios_name] but it doesn't work.

 
Thanks
1 Accepted Solution

Accepted Solutions

If you are looking for some examples on working with interfaces, I have some here:

https://github.com/NSO-developer/nso-5-day-training/blob/master/nso_python_api_examples.py#L224

Here is an example printing an inteface name, the name is a Python attribute of the object, so need to do "interface.name" to get the key. 

 

def print_interfaces(device_name, interface_type):
    """
    Prints each interface number on the device of the given type
    """
    with ncs.maapi.single_write_trans('admin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        device = root.devices.device[device_name]
        for interface in device.interface[interface_type]:
            print (interface.name)

View solution in original post

3 Replies 3

yfherzog
Cisco Employee
Cisco Employee

As you noticed, trying to print a list, just returns the list name.

You can either iterate the list or use python list comprehension to get a list of interface types.

Something like the code below should work (I didn't test this one below):

 

if_types = ['GigabitEthernet', 'Vlan']

for if_type in if_types:
  ifs = [x.name for x in list(device.config.ios__interface[if_type])]
  print(if_type)
  print(ifs)

If you are looking for some examples on working with interfaces, I have some here:

https://github.com/NSO-developer/nso-5-day-training/blob/master/nso_python_api_examples.py#L224

Here is an example printing an inteface name, the name is a Python attribute of the object, so need to do "interface.name" to get the key. 

 

def print_interfaces(device_name, interface_type):
    """
    Prints each interface number on the device of the given type
    """
    with ncs.maapi.single_write_trans('admin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        device = root.devices.device[device_name]
        for interface in device.interface[interface_type]:
            print (interface.name)

Thank you so much  yfherzog and Jason Belk  for your support