[python]working with pan-python module. Module that calls Palo Alto API

I found the example of usage in here. The code will not work on python3. In fact i am not sure why the print(credentials) in the code is necessary, there is no arg passed into the PanXapi which puzzles me why the code will work.

To pass dictionary of arguments into the PanXapi method you will need double asterix.

So here’s the modification for python3, actually i have not tried on python2.7 yet, but i dun think it mattered to me since python3 will be de facto standard henceforth.

import pan.xapi

def get_pan_credentials():
 cred = {}
 cred['api_username'] = "admin"
 cred['api_password'] = "admin"
 cred['hostname'] = ""
 return cred

credentials = get_pan_credentials()
xapi = pan.xapi.PanXapi(**credentials)
xapi.op(cmd='show system info', cmd_xml=True)

The output is the same as using the requests module to call the REST API directly. the difference with Pan-Python is that passing arguments is made easy.
So here’s the complete output:

<system><hostname>PA-VM</hostname><ip-address></ip-address><netmask></netmask><default-gateway></default-gateway><is-dhcp>yes</is-dhcp><ipv6-address>unknown</ipv6-address><ipv6-link-local-address>fe80::5200:ff:fe01:0/64</ipv6-link-local-address><ipv6-default-gateway /><mac-address>50:00:00:01:00:00</mac-address><time>Sun Nov 12 00:48:47 2017
<uptime>0 days, 21:49:26</uptime>

4 thoughts on “[python]working with pan-python module. Module that calls Palo Alto API

  1. Hi Cyrus,

    Thanks for this post, it’s only a little one but I had been struggling with getting PanPython working and interacting with the API for security rules.

    I am looking at how I can bulk edit the assigned security profile group assigned to 900 pre rules in Panorama across 2 device groups. Unless I want to click through every damn rule, I believe using a Python script is going to be the only way to bulk do every rule where the current action is Allow and apply the specific security profile group.

    Anyway thanks again for the great easy article to read, all the best.


    1. Hi Daniel,
      I would recommend you to use the API reference within the Palo Alto firewall. the PanPython is only a wrapper that makes it “easy” because scripter does not need to known xml, I would recommend you to really use it and try to understand the xml structure with postman first then you can use a template engine such as jinja2 or mako template in your python script to build up the xml post request.

      1. Hi Cyrus,

        This is a good point as I have had some challenges trying to use the pan-python to achieve what I am after. I had started using postman to do this already as well but I am stuck at being able to pull all active rules running in a particular device group and the API browser does not seem to contain it.

        I have however seen in Palo’s documentation that their 9.0 restapi seems to have commands there to pull the security rule base. Is this correct or am I missing something somewhere?


      2. Not sure about the command line though, but the api itself can query for security rules and objects. If only command line can be used try netmiko module I think netmiko has Palo Alto connection.

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