cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
6227
Views
25
Helpful
5
Replies

Execute show command from NSO Action

ian.scheidler1
Level 4
Level 4

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.

3 Accepted Solutions

Accepted Solutions

vleijon
Cisco Employee
Cisco Employee

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.

View solution in original post

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.

 

View solution in original post

You are always allowed to use the prefix, but you are required to use the prefix if there are multiple NEDs exposing the same name.

You can either do ios-stats:exec as a string, or you can do something like ios_stats__exec. Using the prefix is good practice, because it means that if someone later decides they need the XR ned, your package will still work!

View solution in original post

5 Replies 5

vleijon
Cisco Employee
Cisco Employee

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.

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.

 

Hi

Thank you, I did.

So, the reason is probably that you have multiple neds loaded which provide an exec element.

The documentation is not super clear, but if you look in the nso development guide in the chapter “Maagic Examples” there are a few variants of examples that might be helpful.

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'?

You are always allowed to use the prefix, but you are required to use the prefix if there are multiple NEDs exposing the same name.

You can either do ios-stats:exec as a string, or you can do something like ios_stats__exec. Using the prefix is good practice, because it means that if someone later decides they need the XR ned, your package will still work!