09-21-2018 05:17 AM
Hi,
am trying to write an action (I actually want to do this from a service later on, just writing a simple Action to try it out first) which goes to an IOS device and fetches the cdp neighbor infos from it.
I am aware that I can call "devices device ios0 live-status ios-stats:exec show cdp neighbors" or "devices device ios0 config exec "do show cdp neighbors" " (when in configure terminal mode) directly from the NSO CLI.
In my main.py I try the folllowing but already assumed it probably won't work:
with ncs.maapi.single_read_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) neighborinfo = root.devices.device[input.dev]['live-status']['ios-stats:exec show cdp neighbors']
And, as assumed it doesnt work (Error: Python cb_action error. 'ios-stats:exec show cdp neighbors')
I have also tried:
neighborinfo = root.devices.device[input.dev]['live-status']['ios-stats:exec']['show cdp neighbors']
That also didnt work.
How can I access the info from show commands via the Python API? Or is there a better way altogether to do this?
Thanks in advance for your help.
Solved! Go to Solution.
09-21-2018 07:18 AM
I think your approach is sensible. But, calling actions can be a little bit tricky sometimes, because the parameters have to be sent in a special format. This is documented in the API documentation and examples if you look carefully, but, here is an example that works where I call the RPC:
devs = root.devices.device
show = devs[devname].live_status.__getitem__('exec').show inp = show.get_input() # Check OSPF settings inp.args = ['ip ospf neigh'] r = show.request(inp) self.log.info('Show OSPF: {}'.format(r.result))
It runs "show ip ospf neigh". Hopefully this is a helpful starting point.
Two things to note, first we use get_input() to get the input structure that we fill in with arguments. Potentially this could be much more complex than a single "args" string, secondly it uses a trick to get around the fact that exec is a built-in in python3 - there might well be a prettier way around that.
09-21-2018 08:09 AM
Hi,
thanks for the reply first of all (Hope you had a good flight back from Frankfurt last week).
I am now trying this code (just want to get version for now, but could just as well use 'cdp neighbor' as args), which is almost an exact copy of the code you provided:
with ncs.maapi.single_read_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) devs = root.devices.device show = devs[input.dev].live_status.__getitem__('exec').show inp = show.get_input()
inp.args = ['version'] r = show.request(inp) self.log.info('Show Version: {}'.format(r.result))
Unfortunately this gives me a key error. In NSO CLI I get: Error: Python cb_action error. 'exec'
I had to change 'exec' to 'ios-stats:exec' ...like this:
devs[input.dev].live_status.__getitem__('ios-stats:exec').show
And then it worked like a charm, thank you very much!!!
Could you please point me to the part of the Docummentation where this is explained? I seem to be missing it.
09-24-2018 01:09 AM
09-21-2018 07:18 AM
I think your approach is sensible. But, calling actions can be a little bit tricky sometimes, because the parameters have to be sent in a special format. This is documented in the API documentation and examples if you look carefully, but, here is an example that works where I call the RPC:
devs = root.devices.device
show = devs[devname].live_status.__getitem__('exec').show inp = show.get_input() # Check OSPF settings inp.args = ['ip ospf neigh'] r = show.request(inp) self.log.info('Show OSPF: {}'.format(r.result))
It runs "show ip ospf neigh". Hopefully this is a helpful starting point.
Two things to note, first we use get_input() to get the input structure that we fill in with arguments. Potentially this could be much more complex than a single "args" string, secondly it uses a trick to get around the fact that exec is a built-in in python3 - there might well be a prettier way around that.
09-21-2018 08:09 AM
Hi,
thanks for the reply first of all (Hope you had a good flight back from Frankfurt last week).
I am now trying this code (just want to get version for now, but could just as well use 'cdp neighbor' as args), which is almost an exact copy of the code you provided:
with ncs.maapi.single_read_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) devs = root.devices.device show = devs[input.dev].live_status.__getitem__('exec').show inp = show.get_input()
inp.args = ['version'] r = show.request(inp) self.log.info('Show Version: {}'.format(r.result))
Unfortunately this gives me a key error. In NSO CLI I get: Error: Python cb_action error. 'exec'
I had to change 'exec' to 'ios-stats:exec' ...like this:
devs[input.dev].live_status.__getitem__('ios-stats:exec').show
And then it worked like a charm, thank you very much!!!
Could you please point me to the part of the Docummentation where this is explained? I seem to be missing it.
09-23-2018 09:23 AM
09-24-2018 01:01 AM
Thank you. I will take a closer look in the Docs.
And yes, I have also got the IOS XR NED loaded.
So is it normal that I have to call use 'ios-stats:exec' instead of just 'exec' because I have another NED loaded (XR)?
It turns outthe customer doesnt need the XR NED after all, so should I get rid of it and have my code use 'exec' instead of 'ios-stats:exec'?
09-24-2018 01:09 AM
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