Last active
September 9, 2024 13:39
-
-
Save tmanternach/21e3bba23261cbd5bacba2e50588acf6 to your computer and use it in GitHub Desktop.
This python script is used to quickly retrieve the DHCP leases from a Palo Alto firewall. It outputs them as a JSON object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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. | |
Usage: | |
- 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'. | |
Example: | |
$ python panos_dhcp.py | |
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: | |
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) | |
load_dotenv() | |
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__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment