吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 770|回复: 15
收起左侧

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

[复制链接]
huxiaokui 发表于 2024-4-23 21:43
本帖最后由 huxiaokui 于 2024-4-24 16:16 编辑

4/24更新,
保存的文件名后面添加时间,便于自动保存多个版本
所有代码如下
[Python] 纯文本查看 复制代码
from datetime import datetime
# 获取当前时间
now=datetime.now()[attach]2691554[/attach]
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:}
2.png



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

1.png



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

4.png



所以求助变成了分享,或许哪位就用上了呢
代码如下:
[Python] 纯文本查看 复制代码
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~~~~")

3.png


免费评分

参与人数 5吾爱币 +4 热心值 +4 收起 理由
461735945 + 1 + 1 用心讨论,共获提升!
lyrong2008 + 1 谢谢@Thanks!
shengruqing + 1 我很赞同!
stiller + 1 + 1 用心讨论,共获提升!
QvQsuipian + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

小和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
欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Lty20000423 发表于 2024-4-24 07:36
52有你更精彩
单车坐垫 发表于 2024-4-24 07:38
感谢分享
zbking1314 发表于 2024-4-24 07:59
谢谢分享,来学习一下,下载备用
lyrong2008 发表于 2024-4-24 08:11
感谢分享思路
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
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 23:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表