DDoS Detection, Mitigation, Orchestration, and Threat Intelligence
Consolidated Security & CGNAT
TLS/SSL Inspection
Web Application Firewall
Application Security & Load Balancing
Analytics & Management
CGNAT & IPv6 Migration
In previous Python + A10 aXAPI posts, the script was written from the ground up with only 2.7 Standard Libraries. A10’s HTTP API makes it easy to interact with in almost any languages, Python included.
Another way to interact with the A10 device with Python can be with the A10 Python SDK. It can be downloaded from the support section under 3rd party SDK.
Here I try to show some examples using the AX Python SDK. I am using IPython for its ability to easily show the available methods and attributes. But it is not required. The interactive prompt are executed under the directory as the SDK to import the SDK modules.
If you prefer, we provide the HTML format of the axAPI documentation.
The examples below are taken from the main.py script included in the SDK..
Here we go:
# ipython Python 2.7.5 (default, Oct 8 2013, 12:19:01) Type "copyright", "credits" or "license" for more information. IPython 0.13.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import method_call
In [2]: dir(method_call) Out[2]: ['AXAPI_DEVICE', 'AXAPI_LOGIN', 'AXAPI_SESSION_ID', 'AxAPIError', 'AxApiContext', 'AxError', 'AxObject', 'REST_URL', 'XML', '_XmlDict', '_XmlList', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_get_request_url', '_send_request', '_set_session_id', 'call_api', 'connect_patched', 'httplib', 'json', 'socket', 'ssl', 'urllib', 'urllib2'] In [3]: help(method_call) Help on module method_call: NAME method_call - method_call module. FILE /home/echou/SDK-Python-aXAPI/method_call.py DESCRIPTION This module is used to perform the calls to the AX RESTful interface. Author: Richard Zhang, A10 Networks (c) e-mail: [email protected] Date : 03/04/2012
In [4]: a10Obj = method_call.AxApiContext("192.168.148.161", "admin", "a10").authentication() username=admin&password=a10&method=authenticate https://192.168.148.161:443/services/rest/V2/ <?xml version="1.0" encoding="utf-8" ?><response status="ok"><session_id>19b69d604c5bf0b27d79595b1b50e3</session_id></response>
In [5]: dir(a10Obj) Out[5]: ['__class__', '__delattr__', '__dict__', '__display__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', '__new__', '__obj_name__', '__obj_readonly__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '__xml_convrt__', '_appendString', '_generateDictInUrl', '_generateListInUrl', '_set_properties', 'authentication', 'device_ip', 'dump', 'get', 'getInfo', 'getObjectDict', 'getRequestPostDataJson', 'getRequestPostDataXml', 'is_login', 'password', 'session_id', 'switchContext', 'username'] In [6]: In [7]: a10Obj.username Out[7]: 'admin' In [8]: a10Obj.session_id Out[8]: '19b69d604c5bf0b27d79595b1b50e3' In [9]: a10Obj.getInfo Out[9]: <bound method AxApiContext.getInfo of AxApiContext(session_id = '19b69d604c5bf0b27d7..., is_login = True, device_ip = '192.168.148.161', username = 'admin')> In [10]:
In [10]: from slb import VirtualServer
In [11]: vip_list = VirtualServer. <<< Press [tab] here VirtualServer.create VirtualServer.getObjectDict VirtualServer.delete VirtualServer.getRequestPostDataJson VirtualServer.dump VirtualServer.getRequestPostDataXml VirtualServer.get VirtualServer.mro VirtualServer.getAll VirtualServer.searchByName VirtualServer.getInfo VirtualServer.update
In [14]: vip_list = VirtualServer.getAll() method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json https://192.168.148.161:443/services/rest/V2/ {"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]}]} In [15]: for vip in vip_list: ....: print vip ....: VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1) In [16]:
In [16]: vip_new = VirtualServer(name="vip2", address="2.2.2.2") In [17]: vip_new.create() {"virtual_server": {"name": "vip2", "address": "2.2.2.2"}} https://192.168.148.161:443/services/rest/V2/?method=slb.virtual_server.create&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json {"response": {"status": "OK"}} Out[17]: 0 In [18]:
In [19]: vip_list = VirtualServer.getAll() method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json https://192.168.148.161:443/services/rest/V2/ {"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]},{"name":"vip2","address":"2.2.2.2","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[]}]} In [20]: for vip in vip_list: ....: print vip ....: VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1) VirtualServer(name = 'vip2', address = '2.2.2.2', status = 1)
In [21]: vip_del = VirtualServer(name='vip2') In [22]: vip_del. vip_del.create vip_del.getAll vip_del.getRequestPostDataXml vip_del.delete vip_del.getInfo vip_del.name vip_del.dump vip_del.getObjectDict vip_del.searchByName vip_del.get vip_del.getRequestPostDataJson vip_del.update In [23]: vip_del.delete() session_id=19b69d604c5bf0b27d79595b1b50e3&method=slb.virtual_server.delete&name=vip2&format=json https://192.168.148.161:443/services/rest/V2/ {"response": {"status": "OK"}} Out[23]: 0
In [24]: for vip in VirtualServer.getAll(): ....: print vip ....: method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json https://192.168.148.161:443/services/rest/V2/ {"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]}]} VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1) In [25]:
There you have it, A10 AX Python SDK in action. Nice and easy. If the method is available in the SDK, why reinvent the wheel? Even if it is not, you can always write your own method based on the SDK.
Leave me comments and let me know if there are other examples that you would like to see.
Cheers!