huxiaokui 发表于 2024-4-23 21:43

使用Python导出paloalt防火墙配置,实现自动化备份

本帖最后由 huxiaokui 于 2024-4-24 16:16 编辑

4/24更新,
保存的文件名后面添加时间,便于自动保存多个版本
所有代码如下

from datetime import datetime
# 获取当前时间
now=datetime.now()
print(now)
# 格式化时间为字符串,例如 2023-03-21_15-26-18
time_str = now.strftime("%Y-%m-%d_%H-%M-%S")
# 输出文件名,将时间字符串作为文件名的一部分
filename = f"paloalt_autoback_{time_str}.xml"
firewall_ip = "192.168.***"
username = "adminsad"
password = "admin123321"

# Get the API key
api_key_url = f"https://{firewall_ip}/api/?type=keygen&user={username}&password={password}"
response = requests.get(api_key_url, verify=False)
xml_response = ET.fromstring(response.text)
api_key = xml_response.find("./result/key").text

# Export the configuration

export_url = f"https://{firewall_ip}/api/?type=export&category=configuration&key={api_key}"

config_response = requests.get(export_url, verify=False)

# Save the configuration to a local file

with open(filename, "w",encoding='utf-8') as config_file:
config_file.write(config_response.text)

#print({config_response.text})
print("OK~~~~")



-----------------------------4/23-----------------------------------------




此事说来话长..其实我都准备求助了的{:1_907:}




之前导出来是这个鬼样子,实在难以入目...





就在准备发悬赏贴的时候突然灵光一闪,欸! 解决了(加了个这个encoding='utf-8'):lol:lol





所以求助变成了分享,或许哪位就用上了呢
代码如下:
import requests
import xml.etree.ElementTree as ET
ip = "10.11.1.250"
user = "paxtd"
pwd = "fxgett*%1fF3"
api_url = f"https://{ip}/api/?type=keygen&user={user}&password={pwd}"
response = requests.get(api_url, verify=False)
xml_response = ET.fromstring(response.text)
api_key = xml_response.find("./result/key").text

export_url = f"https://{ip}/api/?type=export&category=configuration&key={api_key}"
xml_response = requests.get(export_url, verify=False)

with open("ini.xml", "w",encoding='utf-8') as config:
config.write(xml_response.text)
print("OK~~~~")



小和00 发表于 2024-4-24 10:22

    import requests
ModuleNotFoundError: No module named 'requests'

哪位大师能帮忙回答一下 这是缺啥文件了吗

pj666a 发表于 2024-4-23 22:03

sototo 发表于 2024-4-24 00:04

欢迎分析讨论交流,吾爱破解论坛有你更精彩!{:301_978:}

Lty20000423 发表于 2024-4-24 07:36

52有你更精彩

单车坐垫 发表于 2024-4-24 07:38

感谢分享

zbking1314 发表于 2024-4-24 07:59

谢谢分享,来学习一下,下载备用

lyrong2008 发表于 2024-4-24 08:11

感谢分享思路{:1_893:}

zero77779 发表于 2024-4-24 09:13

不错不错,最近在学习python

huxiaokui 发表于 2024-4-24 10:26

小和00 发表于 2024-4-24 10:22
import requests
ModuleNotFoundError: No module named 'requests'



先安装requests
pip install requests
页: [1] 2
查看完整版本: 使用Python导出paloalt防火墙配置,实现自动化备份