Export Palo Alto firewall config without Panorama with Python

Previously I was using powershell script for exporting PA config, now I attempted to write one with Python, took me a while to read data from the xml tag… I was using BeautifulSoup4 library to read the data from the tags easily.

Here’s the code with comments

import requests
from bs4 import BeautifulSoup as BS
import os.path
import datetime
r = requests.get("https://192.168.1.12/api/?type=keygen&user=admin&password=admin", verify=False)
#get the xml from http response
soup = BS(r.text,"lxml")
#get the key data from 
key = soup.find('key').text

#Get the config using PA expot config API
r = requests.get("https://192.168.1.12/api/?type=export&category=configuration&key={}".format(key), verify=False )
#define pathname
path = 'd:/temp/'
current_date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
#define file name
filename = os.path.join(path, "config" + current_date + ".xml")
#Open a new file config.xml, as defined above
config = open(filename, "w")
#write the response to the config.xml, content must be in string so use r.text
config.write(r.text)
config.close()

Use the crontab to run this script, if you are using Windows Task scheduler you need to have python3 installed.

Advertisement

One thought on “Export Palo Alto firewall config without Panorama with Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s