05-30-2020 01:48 AM - edited 05-30-2020 08:20 AM
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 ?
-
---output ---
GigabitEthernet
Vlan
i tried to follow the configuration path like [name] or [ios_name] but it doesn't work.
Solved! Go to Solution.
06-01-2020 10:54 AM
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)
05-31-2020 12:18 AM
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)
06-01-2020 10:54 AM
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)
06-01-2020 11:06 AM
Thank you so much yfherzog and Jason Belk for your support
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: