06-11-2020 03:30 AM
Hello,
I was able to insert variable in xpath using python maapi but not for all parameters as below:
myname = 'cisco'
myinterface = 'GigabitEthernet'
mynumber = '1'
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)
mydevice = root.devices.device[myname] -- ok
interface = devs.config.ios__interface.GigabitEthernet[mynumber] -- ok
interface = devs.config.ios__interface.myinterface[mynumber] -- NOT ok, tried myinterface with square/curly brackets but no luck.
Can you assist ?
Solved! Go to Solution.
06-11-2020 06:21 AM
The basic problem is: How would Python know that GigabitEthernet is a reference to a field, but myinterface is a reference to a variable that should be resolved? Python solves this by always assuming it is a field reference. You do, however, have a function getattr that you can use.
Something like this works:
def ifindepend(device, iftype, ifnum): with maapi.single_read_trans('ifindepend', 'system') as t: root = ncs.maagic.get_root(t) device = root.devices.device[device] interfaces = device.config.interface interfacetype = getattr(interfaces, iftype) interface = interfacetype[ifnum] print("IF: {} {}".format(interface, interface._path))
06-11-2020 02:40 PM
Yes, that is expected, since that is the name of the list we are in. You can use that _path to get the full path, or just keep grabbing elements under it as normal.
06-11-2020 06:21 AM
The basic problem is: How would Python know that GigabitEthernet is a reference to a field, but myinterface is a reference to a variable that should be resolved? Python solves this by always assuming it is a field reference. You do, however, have a function getattr that you can use.
Something like this works:
def ifindepend(device, iftype, ifnum): with maapi.single_read_trans('ifindepend', 'system') as t: root = ncs.maagic.get_root(t) device = root.devices.device[device] interfaces = device.config.interface interfacetype = getattr(interfaces, iftype) interface = interfacetype[ifnum] print("IF: {} {}".format(interface, interface._path))
06-11-2020 11:46 AM
06-11-2020 02:40 PM
Yes, that is expected, since that is the name of the list we are in. You can use that _path to get the full path, or just keep grabbing elements under it as normal.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide