How to get information from the Netscaler using Python and Suds

Citrix has a ton of documentation for Perl/C/C# and Java ( None for Python ). SOAP client support has not been the best for Python, but is getting better.
If you have soappy/ZSI, which have had issues with regards to consuming Broken WSDL files ( Having to fix the Broken WSDL file manually, SUCKS!).

Enter SUDS. This module is by far, the best SOAP client for Python.
There documentation is simple and straight to the point, with a bunch of nice samples to get you on your way.

Install SUDS before proceeding further. You can get suds from here. https://fedorahosted.org/suds/

Connection to my netscaler was as simple as this…

Python 2.4.3 (#1, Sep  3 2009, 15:37:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from suds.client import Client
>>> url = "https://nsip/api/NSConfig.wsdl"
>>> from suds.xsd.doctor import *
>>> imp = Import('https://schemas.xmlsoap.org/soap/encoding/')
>>> imp.filter.add("urn:NSConfig")
>>> d = ImportDoctor(imp)
>>> client = Client(url, doctor=d, location="https://nsip/soap/")
>>> client.service.login(username="login", password="pass")
(simpleResult){
rc = 0
message = "Done"
}
>>>

As you can see above,  here we used the xsd doctor to import Soap encoding URL and added a filter for the Netscaler URN.
If you do not import the SOAP encoding schema, this is the error you will receive..

suds.TypeNotFound: Type not found: ‘(Array, https://schemas.xmlsoap.org/soap/encoding/, )

Once you type the above, you can now run commands in the python shell. for instance…

  • client.service.getlbvserver()
  • client.service.getservice()
  • client.service.getservicegroup()

I am attaching a script that I have written, to get Load Balanced Virtual Servers and its Services and ServiceGroups.
This script can also just list all the Virtual Servers, Services, or Service Groups. Examples below..

python queryns.py -c "ZenossTest2_svc" -n "nsip" -u "login" -p "pass"

Service Name Server Name Status IPAddress Port Protocol
ZenossTest2_svc zenossTest OUT OF SERVICE 192.168.101.221 9090 HTTP

python queryns.py -s foobar_test -n "nsip" -u "login" -p "pass"

Virtual Server State IPAddress Port Protocol
foobar_test UP 192.168.101.19 55555 HTTP

ServiceGroup Name SG State Server Name Status IPAddress Port Protocol
"Foo Bar" ENABLED billyboy UP 192.168.101.31 8080 HTTP
"Foo Bar" ENABLED zenossTest UP 192.168.101.221 8080 HTTP

Service Name Server Name Status IPAddress Port Protocol
ZenossTest2_svc zenossTest OUT OF SERVICE 192.168.101.221 9090 HTTP
------------------------------------------------------------------------------------------------------------------------

All the options are here..

python queryns.py -h
usage: queryns.py [options] arg --username=username --password=password --netscaler=netscalerip

options:
-h, --help show this help message and exit
-n NETSCALER, --netscaler=NETSCALER
Here you will put the netscaler IPAddress or the
netscaler hostname
-u USERNAME, --username=USERNAME
Your username
-p PASSWORD, --password=PASSWORD
Your password
-l LIST, --list=LIST List all the names of the Virtual Servers, Services,
ServiceGroups. Example --list=lbvserver,
--list=service, --list=servicegroup
-s SERVER, --vserver=SERVER
Virtual Server that you want to query. You can choose
a Virtual Server or you can pass all, so you get all
the Virtual Servers and its associated
ServiceGroups or Services. Example....
--vserver="foobar_test" or --vserver="all"
-g SGROUP, --sgroup=SGROUP
ServiceGroup that you want the info from
-c SERVICE, --service=SERVICE
Service that you want the info from

Source