Вы находитесь на странице: 1из 11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity
Search

A10NetworksaXAPIwithPython
PostedonOctober3,2013byEricChou

IrecentlyjoinedA10NetworksProfessionalServicesEngineerbasedinthePacificNorthwest.Being
thePythonmindedNetworkEngineerthatIam,thefirstthingIlookedforishowtousePythonto
automateconfigurationtasks.A10providesanaXAPIinterfacethatutilizesRESTWebservicescalls
tomakeconfigurationchanges.Moreinformationcanbefound
here,http://www.a10networks.com/products/axseriesaXAPI.php.
Hereisaquickoverviewofhowitworks:
1. GetasessionIDviaanHTTPScallwithusernameandpassword.
2. ConstructyourHTTPSpostbodyintheformatspecifiedperguide(XMLorJSon).
3. MakePOSTrequesttothedevice.
Figure1.Sourcehttp://www.a10networks.com/products/axseriesaXAPI.php.

Hereisthecodebrokendownintosectionsinaccordancetotheoverviewatthetop:
Step1.GettheSessionID
#!/usr/bin/envpython
#
#v1,September27,2013
#byEricChou
#
https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

1/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

#Reference:AX_aXAPI_Ref_v220121010.pdf
#
importhttplib,json,urllib,urllib2
#GetsthesessionIDtohost172.31.31.121(Student12)
c=httplib.HTTPSConnection(172.31.31.121)
c.request(GET,/services/rest/V2/?
method=authenticate&username=admin&password=a10&format=json)
response=c.getresponse()
data=json.loads(response.read())
session_id=data['session_id']
printSessionCreated.SessionID:+session_id
Step2.ConstructtheHTTPPostbody
NotethatIreusethesessionIDbecauseithasnottimedoutyet.Iskiptherepeatedstepsto
createmorebecausetheonlythingthatisdifferentistheHTTPbody.Fullscriptisattheendof
thepost.
###############################
#Step1.CreateServers#
###############################
#Createslbserverss110.0.2.128
#ConstructHTTPURLandPostBody
post_body=json.dumps(
{server:
{
name:s1,
host:10.0.2.128,
health_monitor:(default),
port_list:[
{"port_num":80,

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

2/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

"protocol":2,}
],
}
}
)
url=https://172.31.31.121/services/rest/V2/?&session_id=+session_id+
&format=json&method=slb.server.create
printURLCreated.URL:+url+body:+post_body
(repeatandrinseforallthenecessaryconfigurationtasks)

Step3.Maketherequest

#Makingrequest
req=urllib2.Request(url,post_body)
rsp=urllib2.urlopen(req)
content=rsp.read()
printResult:+content

Thatisit!Simple,elegant,andrepeatablewithminimaleffort.:)

Hereisthedeviceblankconfigurationbeforescript:

AX12#shrun|sslb
AX12#
AX12#

Hereistheoutputafterexecution:

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

3/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

>>>
SessionCreated.SessionID:69be80219842402d45f0488c52b782
URLCreated.URL:https://172.31.31.121/services/rest/V2/?
&session_id=69be80219842402d45f0488c52b782&format=json&method=slb.server.createbody:{server:
{host:10.0.2.128,name:s1,port_list:[{"protocol":2,"port_num":80}],health_monitor:
(default)}}
Result:{response:{status:OK}}
URLCreated.URL:https://172.31.31.121/services/rest/V2/?
&session_id=69be80219842402d45f0488c52b782&format=json&method=slb.server.createbody:{server:
{host:10.0.2.129,name:s2,port_list:[{"protocol":2,"port_num":80}],health_monitor:
(default)}}
Result:{response:{status:OK}}
URLCreated.URL:https://172.31.31.121/services/rest/V2/?
&session_id=69be80219842402d45f0488c52b782&format=json&method=slb.service_group.createbody:
{service_group:{protocol:2,name:http,member_list:[{"port":80,"server":"s1"},{"port":80,
"server":"s2"}],health_monitor:ping}}
Result:{response:{status:OK}}
URLCreated.URL:https://172.31.31.121/services/rest/V2/?
&session_id=69be80219842402d45f0488c52b782&format=json&method=slb.virtual_server.createbody:
{virtual_server:{subnet:{mask_len:24,address:10.0.1.122},vport_list:[{"service_group":"http",
"protocol":2,"port":80}],name:vip1}}
Result:{response:{status:OK}}
>>>

Deviceconfigurationafter:

AX12#shrun|sslb
slbservers110.0.2.128
connlimit8000000nologging
port80tcp
slbservers210.0.2.129
connlimit8000000nologging

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

4/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

port80tcp
slbservicegrouphttptcp
healthcheckping
members1:80
members2:80
slbvirtualservervip110.0.1.122/24
port80tcp
servicegrouphttp
AX12#
AndnowIcanseethefantasticwebpagebymakingarequesttotheVIPservicedbys1:

aXAPIisprettycool.Iwillprobablyplaywithitmoreandpostmyprogresshere.

HappyCoding!LeavemeacommentonhowyoudliketoseeaXAPIintegrationscripts.:)

Hereisthefullscript:
https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

5/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

Note.Asyoucansee,thenextsteptooptimizewouldbetomaketheHTTPbodyconstructa
functiontocutdownthecode.Alsousemultiprocesstothreadwouldbenicetoo.

#!/usr/bin/envpython

#
#v1,September27,2013
#byEricChou
#
#Reference:AX_aXAPI_Ref_v220121010.pdf
#

importhttplib,json,urllib,urllib2

#GetsthesessionIDtohost172.31.31.121(Student12)
c=httplib.HTTPSConnection(172.31.31.121)
c.request(GET,/services/rest/V2/?
method=authenticate&username=admin&password=a10&format=json)
response=c.getresponse()
data=json.loads(response.read())
session_id=data['session_id']
printSessionCreated.SessionID:+session_id

###############################
#Step1.CreateServers#
###############################

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

6/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

#Createslbserverss110.0.2.128
#ConstructHTTPURLandPostBody
post_body=json.dumps(
{server:
{
name:s1,
host:10.0.2.128,
health_monitor:(default),
port_list:[
{"port_num":80,
"protocol":2,}
],
}
}
)
url=https://172.31.31.121/services/rest/V2/?&session_id=+session_id+
&format=json&method=slb.server.create
printURLCreated.URL:+url+body:+post_body

#Makingrequest
req=urllib2.Request(url,post_body)
rsp=urllib2.urlopen(req)
content=rsp.read()
printResult:+content

#Createslbservers210.0.2.129
post_body=json.dumps(

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

7/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

{server:
{
name:s2,
host:10.0.2.129,
health_monitor:(default),
port_list:[
{"port_num":80,
"protocol":2,}
],
}
}
)
url=https://172.31.31.121/services/rest/V2/?&session_id=+session_id+
&format=json&method=slb.server.create
printURLCreated.URL:+url+body:+post_body

#Makingrequest
req=urllib2.Request(url,post_body)
rsp=urllib2.urlopen(req)
content=rsp.read()
printResult:+content

####################################
#Step2.CreateServiceGroup#
####################################

#Createservicegrouphttpwithmemberservers1ands2

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

8/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

post_body=json.dumps(
{service_group:
{
name:http,
protocol:2,
health_monitor:ping,
member_list:[
{"server":"s1",
"port":80,},
{"server":"s2",
"port":80,},
],
}
}
)
url=https://172.31.31.121/services/rest/V2/?&session_id=+session_id+
&format=json&method=slb.service_group.create
printURLCreated.URL:+url+body:+post_body

#Makingrequest
req=urllib2.Request(url,post_body)
rsp=urllib2.urlopen(req)
content=rsp.read()
printResult:+content

#####################################
#Step3.CreateVirtualServer#

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

9/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

#####################################

#Createvirtualserver
post_body=json.dumps(
{virtual_server:
{
name:vip1,
subnet:
{
address:10.0.1.122,
mask_len:24,
},
vport_list:[
{"protocol":2,
"port":80,
"service_group":"http",
},
],
}
}
)
url=https://172.31.31.121/services/rest/V2/?&session_id=+session_id+
&format=json&method=slb.virtual_server.create
printURLCreated.URL:+url+body:+post_body

#Makingrequest
req=urllib2.Request(url,post_body)

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

10/11

21/01/2015

A10NetworksaXAPIwithPython|A10NetworksVirtualADCCommunity

rsp=urllib2.urlopen(req)
content=rsp.read()
printResult:+content

ThisentrywaspostedinBlogPostsandtaggedA10Networks,A10ThunderSeries,AXSeries,aXAPI,HTTPS,Python.Bookmarkthepermalink.

Copyright2014A10Networks'VirtualADC(ApplicationDeliveryCommunity)AllRightsReserved.|PrivacyPolicy|TermsofService

https://www.a10networks.com/vadc/index.php/a10networksaxapiwithpython/

11/11

Вам также может понравиться