上网行为管理不提供自动备份功能,为了防止设备故障后无法恢复,需要实现自动化的离线备份,希望通过自动化脚本来实现备份功能 点击系统管理->系统配置->配置备份与恢复 在浏览器中按F12,并点击Network查看网络请求信息 点击下载当前配置,此时可以看到已经出现了大量的请求,主要看一下backupconfig.cgi对应的数据 注意如下几个信息: Request Payload:这个是提交时上传的数据 content-type:格式 X-Requested-With:请求方法 x-sangfor-anticsrf:登录Token 按照上面同样的方法,进入登录画面获取Token信息。 最终完美实现了每天备份。 最终的代码: 红字部分为需要修改的 Username:登录的用户名 Password:密码 网关ID:系统配置里面的授权管理中可以查看 AC13.0.7:系统版本号 #!/usr/bin/env python # -*- coding:utf-8 -*- import sys import requests from json import dumps, loads from decimal import Decimal import time import re,os,commands import logging from jsbn import RSAKey file_path ="/home/ubuntu/runScript/ExportWolf/files/" dates = time.strftime("%Y%m%d",time.localtime()) class AcUser(object): logging.basicConfig(level=logging.INFO, format='%(asctime)s%(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y%H:%M:%S', stream=sys.stdout) #filename='/tmp/acuser.log', #filemode='a') def__init__(self,url,codes): self.url = url self.codes = codes self.pwd = "password" self.base_url = "https:// " + url + " /" def get_timestamp(self): epoch = str(Decimal(time.time())) epoch = epoch.split('.') timestamp = epoch[0] + epoch[1][:3] return timestamp def login(self): login_url = self.base_url + 'cgi-bin/login.cgi' login_data = { "opr" : "rsakey"} headers = { 'content-type' : 'application/json' } self.session = requests.Session() r = self.session.post(login_url,data=dumps(login_data),headers=headers,verify=False) if 'true' in r.content: logging.info("Get RSA Key %s successful" % self.city ) else: logging.info("Get RSA Key %s failed" % self.city ) return False rsa_key = loads(r.content)["key"] rsa = RSAKey() rsa.setPublic(rsa_key, "10001") enc_pwd = rsa.encrypt(self.pwd) login_data = { "opr" : "login", "data" : {"user" : "username" ,"pwd" : enc_pwd } } headers = { 'content-type' : 'application/json' } self.session = requests.Session() r =self.session.post(login_url,data=dumps(login_data),headers=headers,verify=False) if 'true' in r.content: logging.info("Login %s successful" % self.city ) else: logging.info("Login %s failed" % self.city ) return False return True def export_user(self): index_url = self.base_url + 'index.php' r_index = self.session.get(index_url,verify =False) content = r_index.content ret=re.findall('SFAntiCsrfToken = .*',content) Token = ret[0].split('\"')[1] select_url = self.base_url + '/cgi-bin/backupconfig.cgi' export_data = { "opr" : "export" } headers = { 'content-type' : 'application/json', 'X-Requested-With':'XMLHttpRequest','x-sangfor-anticsrf':Token} r =self.session.post(select_url,data=dumps(export_data),headers=headers,verify=False) export_url = self.base_url + 'php/loadfile.php?file=/export/AC13.0.7_' + self.codes + '_' + dates + '.bcf' print export_url r_export = self.session.get(export_url,verify =False) print self.city + '-' + dates + '-' + 'AcBackup.bcf' with open(file_path + self.city + '-' + dates + '-' + 'AcBackup.bcf' ,'wb') as f: f.write(r_export.content) if os.path.exists(file_path + self.city + '-' + dates + '-' +'AcBackup.bcf'): logging.info("Export %s AcBackup.bcf successful" % self.city) else: logging.info("Export %s AcBackup.bcf failed" % self.city) if __name__ == '__main__': dict = {"url1":"网关ID","url2":"网关ID "," url3":"网关ID"} for k,v in dict.items(): url= k codes = v Ac = AcUser(url,codes) if Ac.login(): Ac.export_user() 执行结果: Fri, 12 Mar 2021 09:33:46AcBackup.py[line:50] INFO Get RSA Key sh successful Fri, 12 Mar 2021 09:33:50AcBackup.py[line:68] INFO Login sh successful https://****/php/loadfile.php?file=/export/AC13.0.7_****_20210312.bcf ****-20210312-AcBackup.bcf Fri, 12 Mar 2021 09:34:04AcBackup.py[line:97] INFO Export sh AcBackup.bcf successful |