Question

How do I get started with Python Scripting? Are there any Code examples?

  • 28 October 2021
  • 5 replies
  • 567 views

I’m reasonably familiar with python and other automation frameworks like Ansible. I’m trying replicate existing functionality we have via a python script within Netbrain via a Qapp. My plan is to pull data from devices and to manipulate the output with Python before returning a table/CSV to the user. I’m finding it fairly frustrating to get started with the python element in terms of feeding the data into the python script and outputting the data.

 

Are there any good resources for learning this element or any code samples? The documentation I’ve seen doesn’t appear to focus on this element at all.


5 replies

Userlevel 4
Badge +1

Hi @Kieran Drain,

you probably know the Netbrain’s Github page https://github.com/NetBrainAPI/NetBrain-REST-API-R10
already which explains the API calls with example code.

But you are right, for the stuff you are trying to do, using Python and automation inside NB I also have not been able to find any pages that show the possibilities that exist.

Yes I’ve seen the Github page for the Netbrain API but this seems more geared towards writing external code to utilise Netbrain and to access it via the API rather than actually perform analysis of inputs from within a Qapp and produce useful outputs. There does not even seem to be a ‘nice’ way to return an output from a script.

The python inside scripts doesn’t contain libraries I’d consider essential for network automations such as the requests library.

Userlevel 2
Badge +1

Hi Kieran and Carsten,

Please refer to this link to get started on Qapp Programming in python. https://www.netbraintech.com/docs/ie100a/help/index.html?qapp-api-list.htm

 

If you want to see the modules and functions available in them, use inbuilt inspect module like below. 

from inspect import getmembers, isfunction
import sys, platform

from netbrain.sysapi import devicedata

AddMessage(platform.python_version())

# Get list of all modules available and print them.
modules = sys.modules.keys()
AddMessage(modules)


# Get list of all functions available in netbrain.sysapi.devicedata and print them.
devicedata_functions = getmembers(devicedata, isfunction)

for func, _ in devicedata_functions:
AddMessage(func)

 

Note - For specific use cases, please open a case with Netbrain Support and one of us will help you. Thanks !

To me, unless I’ve just completely missed it, there’s a gap in the documentation on getting started with this - how to actually refer to Qapp elements from within the python script. I don’t know how you’re supposed to actually use the APIs until you know this. I’ve just had to try and figure some of these out by browsing the built-in qapps for script elements.

For example, the device is $_device. A table in your qapp named Table1 is $Table1. So something like:

devObject = $_device
AddMessage(devObject.GetProperty(‘model’))

someTable = $Table1
AddMessage(someTable.GetAttribute('bla'))

Once you understand that piece, the API documentation becomes a whole lot more useful.

Its definitely helpful to put your qapp in debug mode and use AddMessage() to see raw output in the execution log while you’re learning. Then you can use the other APIs to do useful things with your output.

Another one that took me a bit. If you have a table and want to set an attribute or column by using “Variable by formula” (which is just another script), you can set it with something like $cell = ‘bla’

It’d be great if NetBrain could provide us a table of these common references, especially the ones like $cell that would be nearly impossible to guess without browsing other code.

To me, unless I’ve just completely missed it, there’s a gap in the documentation on getting started with this - how to actually refer to Qapp elements from within the python script. I don’t know how you’re supposed to actually use the APIs until you know this. I’ve just had to try and figure some of these out by browsing the built-in qapps for script elements.

For example, the device is $_device. A table in your qapp named Table1 is $Table1. So something like:

devObject = $_device
AddMessage(devObject.GetProperty(‘model’))

someTable = $Table1
AddMessage(someTable.GetAttribute('bla'))

Once you understand that piece, the API documentation becomes a whole lot more useful.

Its definitely helpful to put your qapp in debug mode and use AddMessage() to see raw output in the execution log while you’re learning. Then you can use the other APIs to do useful things with your output.

Another one that took me a bit. If you have a table and want to set an attribute or column by using “Variable by formula” (which is just another script), you can set it with something like $cell = ‘bla’

It’d be great if NetBrain could provide us a table of these common references, especially the ones like $cell that would be nearly impossible to guess without browsing other code.

Thanks Blake  - This is exactly what I was looking for; There was good documentation with the functions and how to handle them but nothing for how to reference the objects presumably created in the preceding steps of the Qapp. 

I think understanding how these objects are presented to python (e.g. as dictionary/tuple etc) is also important to understand so we know how to enumerate them and access the data.

I can appreciate why Netbrain might want to hide the complexity in automations from the average end user and instead build those desired functions into the application however there is sometimes a need for greater agility in network automation especially for one off requirements and this is where python becomes essential. 

Reply


Community |  Ideas

Facebook |  Instagram |  Youtube |  Twitter |  LinkedIn
Privacy & Security Statement  |  Terms & Conditions |  Impressum  |  UK Modern Slavery Statement