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.
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.