I feel like the Citrix ADC Nitro API does not get utilized (or promoted for that matter) far enough in our current IT environments. It’s a great API that enables you to automate many difficult and tedious tasks.
Currently, I am working on creating my own application in Python to help IT admins out with their ADC’s, free to use in a later phase. This process sparked the idea that I should probably blog about it, and maybe set you on your way to writing your own first script or application. It’s fun, really!
First, let me introduce you to the Nitro API. As Citrix puts it in their own documentation:
The Citrix ADC NITRO protocol allows you to configure and monitor the Citrix ADC appliance programmatically by using Representational State Transfer (REST) interfaces. Therefore, NITRO applications can be developed in any programming language. Additionally, for applications that must be developed in Java or .NET or Python, NITRO APIs are exposed through relevant libraries that are packaged as separate Software Development Kits (SDKs).
Any language, how cool is that! I understand most of the sysadmins reading this have at least a fundamental knowledge and need of Powershell. Which I think is a great tool, but limits the potential of the Nitro API. I will be using Python as it has a ton of libraries and is a relatively easy language to learn. If you really prefer some basic setup in Powershell, let me know and I will publish a separate blog on that.
Okay, first things first. I am going to assume you have an IDE with Python running (for example, Pycharm).
Before we start we need to import the Nitro API Python Library, which is fairly simple.
Citrix says the following on their webpage, which left me a bit puzzled:
NITRO APIs are exposed through relevant libraries that are packaged as separate Software Development Kits (SDKs).
But my question immediately was, ‘where’ are the SDK’s exposed?
Installing/importing the Nitro library
You can find the SDK’s in your ADC, which actually makes a lot of sense.
Just login to the GUI and browse to ‘Downloads -> Nitro API SDK for Python’.
Please note that there are different API versions, based on your ADC major release the version might differ.
The latest is the 13.0 version and is that is also the version used in this blog.
A .TGZ file will download, unpack this and open it up.
You will notice a bunch of files, focus on the ‘readme_ start.txt’ file.
This file contains further instructions on how to install the library.
In short, and this is quoted in the readme_start.txt file you need to do the following:
Once you have a copy of the source archive unpacked into a similarly-named directory, you can embed it in your Python package, or install it into your site-packages easily:
$ python setup.py install
Once this is done, you should be all set for importing the library in python.
Setting up a connection
Alright, let’s import our first piece of library now.
from nssrc.com.citrix.netscaler.nitro.service.nitro_service import nitro_service
The import might look quite daunting, but do not worry, it does not bite.
In short, we just imported the ‘nitro_service’ which is used to make a connection to your ADC’s API.
The Nitro_Service works quite straight forward.
First we need to declare it, and then we need to login.
See my code example:
ns_session = nitro_service("yourip","http") ns_session.login("username","password",3600) print(ns_session.isLogin())
Of course, substitute the yourip with your actual IP. The same goes for username/password.
Determine if your ADC works with HTTPS or HTTP requests and use the appropriate protocol.
The 3600 is the session timer, you can change this if you like.
The ns_session.isLogin() will return either True of False based on the fact if you are logged in or not.
If I am right you now have the following in place:
And if all is okay, ‘True’ should be printed to your console. This means you are logged in, great!
From here on out you can experiment with different kinds of commands and scripts.
Be sure to checkout the ‘doc’ folder in your .TGZ file you downloaded earlier, it contains the API reference and some great demo files.
Also be sure to check the Citrix documentation online, as it contains many examples and snippets.
In a following blog, I will demonstrate some fun functionalities that are possible now that you have the connection setup.
Thanks for reading!