19 August, 2021 #Citrix

Citrix ADC SDK Python

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

My “Turbo ADC” application is written in Python to help IT admins out with their ADC’s. 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 tons of fun, really!

Introduction

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 will be using Python whom has the good ‘request‘ library and it has a ton of other libraries that we can combine with the Nitro API (SDK) code.

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 SDK.
You might wonder what the difference is between the API and the SDK.
In short, with the API you work with webrequests and the SDK makes that easier for you.
Without the SDK (source from Citrix), we would need to take care of all this:

Method: tell NITRO what you want to do. Whether you want to retrieve data (GET), add new data (POST), update existing data (PUT) or delete data (DELETE)

URL: specifies NetScaler IP address, whether you address the NetScaler configuration or statistical data, which resource type you want to address and gives you the additional option to specify a resource name or the action you want to perform.

Request Header: provides NITRO with meta data, like the content type for the request body.

Request Body: sends the actual information to NITRO, preferably using a JSON formatted payload

With the SDK, a task like logging in looks like this instead:
ns_session = nitro_service(“[nsip]”, “[protocol]”)
ns_session.login(“[username]”, “[password]”, 3600)


The SDK exposes pre-maderequest so that it’s easier and faster to write code. SDK based code is also more easy to read.

Installing/importing the Python Nitro SDK

You can find the SDK’s in your ADC.
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 at the time of writing 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 the SDK in our script/application.

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.
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 all went well, you now have the following in place:

The ns_session.isLogin() returns a boolean. In the case of True, you are succesfully logged in. Hooray!

From here on out you can experiment with different kinds of commands and scripts and use the ns_session variable.
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!

0 Comments on Citrix ADC SDK Python

Leave a Comment

Your email address will not be published.

You Might Be Interested In