import requests
import re
##因华三防火墙restful不支持移动策略,故单独用netconf编写移动策略
# 头部信息,带上host即可
headers = {
"Accept-Encoding": "gzip,deflate,br",
'Content-Type': 'xml',
'accept': '*/*',
'accept-language': 'zh-CN',
# 'Content-Length': '1024',
# "Host": "h3c防火墙ip",
"Connection": "Keep-Alive",
"User-Agent": "Apache-HttpClient/4.1.1 (java 1.5)",
"X-Requested-With": "XMLHttpRequest"
}
# auth_AuthInfo这段代码主要账号密码转换认证参数xxxxxx,
auth_AuthInfo = '''
账号
密码
zh-cn
urn:ietf:params:netconf:base:1.0
'''
url = "http://防火墙ip:80/soap/netconf/" # 统一url参数,无需改动
req = requests.post(url, headers=headers, data=auth_AuthInfo)
# print(req.text)
# auth_AuthInfo_data_numeral = str(re.findall(r"+S+", req.text)) # 正则取认证参数
# auth_AuthInfo_data_numeral = "".join(re.findall(r"[^[|^]|^']",auth_AuthInfo_data_numeral))
# print(type(auth_AuthInfo_data_numeral))
auth_AuthInfo_data_numeral = re.findall(r"(.*)+.?", req.text) # 正则取认证参数
auth_AuthInfo_data_numeral = '' + auth_AuthInfo_data_numeral[-1] + '' # 取出来的值为list,强制转换为str,认证参数组装为rpc的格式
# 密钥
print(auth_AuthInfo_data_numeral)
print(type(auth_AuthInfo_data_numeral))
ID = str(2)
ID_num = ""+ ID +""
data_remove_sec = """
{}
2
1
"""
# 65512 该id为安全策略的ID号
# 参数 1、参数1:为安全策略移动到最前,禁用策略谨用
# 4、参数4:为安全策略移动到最后
# 5、参数5:向上移动一格。
# 6、参数6:向下移动一格。
# data_remove_sec = data_remove_sec.format(auth_AuthInfo_data_numeral) # 恶心代码,强制封装认证参数
# #
# # #
# req = requests.post(url, data=data_remove_sec, headers=headers)
# req_data = req.text
# # print(req_data) #打印返回数据
# if re.findall(r"ok", req.text) == ['ok']:
#
# print('执行状态(成功显示: ['ok'],失败显示为: [ ])------------->',re.findall(r"ok", req.text),"<------------")
# print('移动策略成功')
# else:
# print(req_data)
# print('-------------检查参数是否有误-------------')