記一次imperva-API開發-python3
阿新 • • 發佈:2018-12-25
有些客戶想要對imperva裝置進行實現自動化管理,imperva官方提供的api介面寫的也非常的詳細,使用的方法就是通過指定的格式向管理平臺傳送http請求進行控制。一下是我簡單寫的一個python3程式碼(我知道自己很菜。。比較中國化勿噴。。。 ^(* ̄(oo) ̄)^)
import requests from requests.packages import urllib3 import base64 import re import time,sys import urllib.request #from selenium import webdriver #定義代理池測試排錯用 ''' proxy_handler = urllib.request.ProxyHandler({ 'https':'https://127.0.0.1:8080', 'http':'http://127.0.0.1:8080' }) opener = urllib.request.build_opener(proxy_handler) ''' #建立cookie的持久連線 connection = requests.Session() #訪問imperva的登陸介面 證書驗證為false urllib3.disable_warnings() #清除https的告警資訊 #登陸的使用者名稱和密碼 user = "admin" password = "Webco123" userandpassword = user+":"+password #登入編碼方法 base64userandpassword = base64.b64encode(userandpassword.encode('utf-8')) decodeuserandpassword = str(base64userandpassword,'utf-8') #定義登入頭部資訊 headers = { "Content-Type":"application/json", "Authorization": "Basic %s"%decodeuserandpassword } #登陸請求 dengluzhuangtai = connection.post('https://192.168.2.100:8083/SecureSphere/api/v1/auth/session',verify=False,headers=headers) #返回結果 response_status_code = dengluzhuangtai.status_code response_text = dengluzhuangtai.text #列印返回碼 if response_status_code==200: print("登陸成功 %s"%response_status_code) else: print("登陸失敗"+print(response_status_code)) #列印返回結果 print(response_text) impervackkoie = re.findall(r'JSESSIONID=.*? ',str(dengluzhuangtai.cookies),re.S)[0] #正則過濾cookie print(impervackkoie) #定義包體資訊 data = { "gatewayPlatform":"", "gatewayMode":"", "failMode":"", "overloadPolicy":"" } #建立伺服器組 ''' set_service_group = connection.post('https://192.168.2.100:8083/SecureSphere/api/v1/conf/serverGroups/qwe/123') #qwe大組123小組 print(set_service_group.status_code) if set_service_group.status_code==200: print("建立成功") elif set_service_group.status_code==406: print("重複建立該伺服器組,請建立其他名字") else: print("位置型別請查詢手冊",set_service_group.status_code,set_service_group.text) ''' #檢視伺服器組 ''' catservicegroup =connection.get('https://192.168.2.100:8083/SecureSphere/api/v1/conf/sites') print(catservicegroup.status_code,catservicegroup.text) ''' #檢視伺服器組內的伺服器 ''' test1 =connection.get('https://192.168.2.100:8083/SecureSphere/api/v1/conf/serverGroups/Default Site') print(test1.status_code,test1.text) ''' #刪除伺服器組 ''' test1 =connection.delete('https://192.168.2.100:8083/SecureSphere/api/v1/conf/serverGroups/qwe/123') print(test1.status_code,test1.text) ''' #建立伺服器組後給伺服器新增站點 ''' data1={ "comment":"123qweasdzxc", # "gateway-group":"gateway123", # "ip":"1.1.1.1" } set_service_group_ip =connection.post('https://192.168.2.100:8083/v1/conf/serverGroups/qwe/123/protectedIPs/192.168.1.1?gatewayGroup=gateway123', data=data1) print("***********") print(set_service_group_ip.status_code,set_service_group_ip.text) ''' #獲取字典 #getdictionaries = connection.get('https://192.168.2.100:8083/conf/dictionaries') #print(getdictionaries.status_code,getdictionaries.text) #建立字典 ##定義包體資訊 '''post1body = { "category":"manual", "description":"qwe", "type":"Web" } setdictionaries = connection.post('https://192.168.2.100:8083/conf/dictionaries/zet',data=post1body) print(setdictionaries.status_code,setdictionaries.text) print("***********")'''
以上程式碼中後面有幾個例子我已經註釋了。但是 #建立伺服器組後給伺服器新增站點 這塊還是有些問題跟著API的方法去寫怎麼也不成功報的錯誤api上面也沒顯示到時候還得問問原廠的工程師。