Skip to content

Instantly share code, notes, and snippets.

Last active November 5, 2023 03:30
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
This python script is used to quickly retrieve the DHCP leases from a Palo Alto firewall. It outputs them as a JSON object.
This script retrieves DHCP leases information from a Palo Alto Networks firewall using its API.
It sends a request to the specified host with an API key to fetch DHCP lease information for a given network interface. The retrieved data is then parsed and printed in JSON format.
- Make sure to set the necessary environment variables for 'HOST', 'KEY', and 'INTERFACE' using a .env file.
- Ensure the required libraries are installed, including 'os', 'requests', 'json', 'xmltodict', 'urllib3', and 'dotenv'.
$ python
Author: Trevor Manternach
Date: 2023-11-04
import os
import requests
import json
from xmltodict import parse, ParsingInterrupted
from urllib3.exceptions import InsecureRequestWarning
from dotenv import load_dotenv
def get_dhcp_leases(host: str, key: str, interface:str ) -> requests.Response:
"""This function makes the API call to the PAN-OS firewall to query for DHCP leases.
url = f"https://{host}/api/?type=op&cmd=<show><dhcp><server><lease><interface>{interface}</interface></lease></server></dhcp></show>&key={key}"
response = requests.get(url,verify=False)
return response
def main() -> None:
dhcp_leases = get_dhcp_leases(os.getenv("HOST"), os.getenv("KEY"), os.getenv("INTERFACE"))
data = parse(dhcp_leases.content)
entries = data['response']['result']
print(json.dumps(entries, indent=4))
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment