记一次imperva-API开发-python3

有些客户想要对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上面也没显示到时候还得问问原厂的工程师。

猜你喜欢

转载自blog.csdn.net/qq_38656841/article/details/85127580