import time
import requests
import json
import re
import urllib3
flie_time ='_'+str(time.strftime('%Y-%m-%d'))
server_ip =['ip1','ip2] ##防火墙ip地址
urllib3.disable_warnings()
for server_ip in server_ip:
url = 'https://{0}:443/rest/api/login'.format(server_ip)
data=json.dumps({ "lang":"zh_CN", "userName":"YXBp","password":"密码" }) ##账号密码Base64转换
response = requests.post(url, data=data,verify=False)
token=response.json().get('result')[0].get('token')
body={"request_type" : "0",}
#---------------2、策略部分,策略头部---------------------------------------------
cookies = {'token': token,'username': 'api','vsysId': '0','role': 'admin','fromrootvsys': 'true',}
headers = {'X-API-Language': 'en', 'Content-Type': 'application/json',}
policy_url1 = 'https://{0}:443/rest/api/currentcfg?moduleName=mgmt&operation=save'.format(server_ip)
response = requests.post(url=policy_url1, headers=headers, data=json.dumps(body), cookies=cookies, verify=False)
body1={"request_type" : "1",}
policy_url2='https://{0}:443/rest/api/currentcfg?moduleName=mgmt&operation=save'.format(server_ip)
response = requests.post(url=policy_url2, headers=headers,data=json.dumps(body1),cookies=cookies,verify=False)
policy_url3 = 'https://{0}:443/rest/api/currentcfg?moduleName=mgmt&operation=export'.format(server_ip)
response = requests.post(url=policy_url3, headers=headers, data=json.dumps(body), cookies=cookies, verify=False)
lujin=response.json().get('result')[0].get('msg')
aa =str(lujin).split('/')[-1]
time.sleep(3)
policy_url4 = 'https://{0}:443/download/bfm{1}/{2}/'.format(server_ip,lujin,aa)
response = requests.get(url=policy_url4, headers=headers, cookies=cookies, verify=False)
with open(server_ip +flie_time, "a+") as f:
f.writelines(response.text)
time.sleep(0.1)