Pan-Python

0.25.0 · active · verified Thu Apr 16

Pan-Python is a multi-tool set for interacting with Palo Alto Networks PAN-OS, Panorama, WildFire, and AutoFocus platforms. It provides a powerful, low-level Python interface to the PAN-OS and Panorama XML API, as well as interfaces for WildFire, AutoFocus, and licensing APIs. The library is actively maintained, with its current version being 0.25.0, and receives regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Palo Alto Networks device (Firewall or Panorama) using `pan.xapi.PanXapi` and execute an operational command (`show system info`). It also includes a commented example for a configuration edit. Ensure you have the hostname/IP and an API key configured as environment variables for secure access.

import os
from pan.xapi import PanXapi, PanXapiError

hostname = os.environ.get('PAN_OS_HOSTNAME', 'your_firewall_ip')
api_key = os.environ.get('PAN_OS_API_KEY', 'your_api_key')

if not hostname or not api_key:
    print("Please set PAN_OS_HOSTNAME and PAN_OS_API_KEY environment variables.")
    exit(1)

try:
    # Initialize the XAPI connection
    xapi = PanXapi(hostname=hostname, api_key=api_key)

    # Example 1: Execute an operational command (show system info)
    print("\n--- Showing System Info ---")
    xapi.op(cmd='show system info', cmd_xml=True)
    if xapi.status == 'success':
        print(xapi.xml_result())
    else:
        print(f"Error fetching system info: {xapi.status}: {xapi.status_detail}")

    # Example 2: Edit a configuration element (e.g., disable a security rule)
    # Note: This is a configuration change and requires appropriate permissions.
    # For a real scenario, ensure rule 'rule7' exists and handle commit.
    print("\n--- Attempting to disable 'rule7' ---")
    xpath = "/config/devices/entry/vsys/entry/rulebase/security/rules/entry[@name='rule7']/disabled"
    element = "<disabled>yes</disabled>"
    # Example: Uncomment and adjust for your environment
    # xapi.edit(xpath=xpath, element=element)
    # if xapi.status == 'success':
    #     print("Rule 'rule7' disabled. Remember to commit changes.")
    # else:
    #     print(f"Error disabling rule: {xapi.status}: {xapi.status_detail}")

except PanXapiError as e:
    print(f"PanXapi Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →