深信服防火墙千呼万唤的API终于在8.0.35版本实现了。具体的API文档可以登录防火墙的后台WEB界面上的右上角点开即可查看:
根本api文档,首先先新建一个api账号,“用户可以登录WebUI通过"系统->管理员账号"栏将默认的账户勾上WEBAPI选项或者新增一个账户并勾选WEB API”
接下来一个小实验,完成api的对接。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import datetime
class FWAPI8035(object):def __init__(self,host,apiuser, passwd):self.host=hosturl = 'https://{}/api/v1/namespaces/@namespace/login'.format(host)headers = {'content-type': "application/json; charset=UTF-8"}body = {"name": apiuser, "password": passwd}response = requests.post(url, data=json.dumps(body), headers=headers, verify=False)responsejson=json.loads(response.content.decode())self.message=responsejson["message"]self.code = responsejson["code"]self.loginResult = responsejson["data"]["loginResult"]self.tocken = self.loginResult["token"]print (self.message)print (self.code)def __del__(self):url = 'https://{}/api/v1/namespaces/@namespace/logout'.format(self.host)headers = {'content-type': "application/json; charset=UTF-8",'Cookie': 'token={}'.format(self.tocken)}response = requests.post(url, headers=headers, verify=False)responsejson=json.loads(response.content.decode())self.message=responsejson["message"]self.code = responsejson["code"]def getpolicy(self,policyname):url = 'https://{0}/api/v1/namespaces/@namespace/appcontrols/policys/{1}'.format(self.host,policyname)headers = {'content-type': "application/json; charset=UTF-8",'Cookie': 'token={}'.format(self.tocken)}response = requests.get(url, headers=headers, verify=False)responsejson=json.loads(response.content.decode())self.message=responsejson["message"]self.code = responsejson["code"]if self.message=='成功':return responsejson["data"]else:Falsedef getpolicylist(self):url = 'https://{0}/api/v1/namespaces/@namespace/appcontrols/policys'.format(self.host)headers = {'content-type': "application/json; charset=UTF-8",'Cookie': 'token={}'.format(self.tocken)}response = requests.get(url, headers=headers, verify=False)responsejson=json.loads(response.content.decode())self.message=responsejson["message"]self.code = responsejson["code"]if self.message=='成功':return responsejson["data"]else:False
上述class是一个简单的例子,init描述了通过具有api权限的账号密码认证后,得到一个token值。后续其他请求需要将Cookie带入到header中。def getpolicylist获取应用策略列表,def getpolicy获取某一个应用策略。