mport json
import hashlib
import requests
import time
#vpn open api 接口地址,替换IP地址即可
VPN_URL = 'https://10.20.20.10:4430/cgi-bin/php-cgi/html/delegatemodule/WebApi.php'
# 生成 sinfor_apitoken
def vpn_get_apitoken(dic, timestamp):
key = 'e08d13a8679ef761a83a9df746b7a44' #替换vpn的key
ss = ''
for i in sorted(dic):
ss = ss + i + '=' + dic[i] + '&'
ss = ss[:-1]
ss = ss + timestamp + key
sinfor_apitoken = hashlib.sha256(ss.encode('utf-8')).hexdigest( )
return sinfor_apitoken
# 获取用户列表
def vpn_get_userlist():
timestamp = str(int(time.time( )))
params = {'action': 'GetSearchData', 'controler': 'User'}
dic = params
dic['timestamp'] = timestamp
sinfor_apitoken = vpn_get_apitoken(dic, timestamp)
headers = {'content-type': "application/x-www-form-urlencoded; charset=UTF-8"}
data = {'timestamp': timestamp, 'sinfor_apitoken': sinfor_apitoken}
req = requests.post(VPN_URL , params = params, data = data, headers = headers, verify = False)
dict_res = json.loads(req.text)
for i in dict_res['result']['data']:
print(i)
#增加vpn用户
def vpn_adduser():
timestamp = str(int(time.time( )))
headers = {'content-type': "application/x-www-form-urlencoded; charset=UTF-8"}
params = {'action': 'AddUserCloud', 'controler': 'User'}
data = {'name': 'testapi2', 'note': '测试api', 'parent_group': '/测试用户组', 'phone': '186XXXXX', 'passwd': '123123yyy', 'role_name': '测试组'}
data['b_inherit_auth'] = '1'
data['b_inherit_grpolicy'] = '1'
dic = dict(params, **data)
dic['timestamp'] = timestamp
sinfor_apitoken = vpn_get_apitoken(dic, timestamp)
data['timestamp'] = timestamp
data['sinfor_apitoken'] = sinfor_apitoken
req = requests.post(VPN_URL, params = params, data = data, headers = headers, verify = False)
dict_res = json.loads(req.text)
print(dict_res)
#删除vpn用户
def vpn_deluser(userlist):
timestamp = str(int(time.time( )))
headers = {'content-type': "application/x-www-form-urlencoded; charset=UTF-8"}
params = {'action': 'DelUserByNameCloud', 'controler': 'User'}
data = {'names': userlist}
dic = dict(params, **data)
dic['timestamp'] = timestamp
sinfor_apitoken = vpn_get_apitoken(dic, timestamp)
data['timestamp'] = timestamp
data['sinfor_apitoken'] = sinfor_apitoken
req = requests.post(VPN_URL, params = params, data = data, headers = headers, verify = False)
dict_res = json.loads(req.text)
print(dict_res)
#获取vpn用户组信息
def vpn_get_grouplist():
timestamp = str(int(time.time( )))
params = {'action': 'GetGroupInfo', 'controler': 'Group'}
data = {'group_name': '/'}
dic = dict(params, **data)
dic['timestamp'] = timestamp
sinfor_apitoken = vpn_get_apitoken(dic, timestamp)
headers = {'content-type': "application/x-www-form-urlencoded; charset=UTF-8"}
data['timestamp'] = timestamp
data['sinfor_apitoken'] = sinfor_apitoken
req = requests.post(VPN_URL, params = params, data = data, headers = headers, verify = False)
dict_res = json.loads(req.text)
for i in dict_res:
print(i, dict_res[i])
# 获取资源列表
def vpn_get_resource():
timestamp = str(int(time.time()))
params = {'action': 'GetRcListDataCloud', 'controler': 'Resource'}
data = {}
dic = params
dic['timestamp'] = timestamp
sinfor_apitoken = vpn_get_apitoken(dic, timestamp)
headers = {'content-type': "application/x-www-form-urlencoded; charset=UTF-8"}
data['timestamp'] = timestamp
data['sinfor_apitoken'] = sinfor_apitoken
req = requests.post(VPN_URL, params = params, data = data, headers = headers, verify = False)
dict_res = json.loads(req.text)
for i in dict_res['result']['data']:
print(i)
替换IP和key后,代码可以直接运行。 |