Enhanced Security Measures in Place:   To ensure a safer experience, we’ve implemented additional, temporary security measures for all users.

Panorama REST API python script examples

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

Panorama REST API python script examples

L0 Member

Hi all,

 

I have been using Panorama's REST API interface lately and I would like to share with you some of the useful python scripts that I have been using in an operational enviroment:

 

Requirements:

1. Install Python software (Windows, Linux or MAC)  

2. Install the following Python modules:

pip install requests -> Allows to execute CURL commands 

pip install urllib3 -> Used to bypass the SSL certificate warning in-case Panorama uses a self-signed certificate

pip instal pprint -> Formats the script's output to a good looking JSON format with proper indexation and spacing

3. Use your favorite source code editor such as Notepad++ or Visual Studio code in order to edit python code (make sure scripts are saved as .py files)

4. Generate a Panorama API KEY. To do that just simply open your favorite browser and enter the following URL:

https://{PANORAMA_IP_ADDRESS}/api/?type=keygen&{api_username}&password={api_user_password}
 
this will generate the Key which you just need simply copy and save it somewhere with you 🙂

 

Scripts:

 

Script # 1 - GET call - List all Zones from one or multiple target templates

 

import requests
import urllib3
import pprint
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

target_templates = ["template1", "template2", "template3"]

headers = {
  'Content-Type': 'application/json',
  'X-PAN-KEY': '{PANORAMA_API_KEY}'
}

for x in target_templates:
    url = "https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1"

    response = requests.request("GET", url, headers=headers, verify=False)

    response_dict = json.loads(response.text)

    print(x)
    pprint.pp(response_dict)

 

 

  Script # 2 - GET call - List any specific Zone from one or multiple target templates

 

import requests
import urllib3
import pprint
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

target_templates = ["template1", "template2", "template3"]

headers = {
  'Content-Type': 'application/json',
  'X-PAN-KEY': '{PANORAMA_API_KEY}'
}

for x in target_templates:
    url = "https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1&name={target_ZONE}"

    response = requests.request("GET", url, headers=headers, verify=False)

    response_dict = json.loads(response.text)

    print(x)
    pprint.pp(response_dict)

 

 

Script #3 - POST call - Create 1 Zones on one or multiple target Templates

 

 

import requests
import urllib3
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

target_templates = ["template1","template2","template3"]

for x in target_templates:

    payload1 = json.dumps({
      "entry": {
        "@name": "{name_of_the_new_zone}",
        "@location": "template",
        "@template": x,
        "@vsys": "vsys1",
        "network": {
          "layer3": {},
          "log-setting": "default"
        }
      }
    })

    url = ("https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_OS_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1&name={name_of_the_new_zone}")
    headers = {
      'Content-Type': 'application/json',
      'X-PAN-KEY': '{PANORAMA_API_KEY}'
    }

    response1 = requests.request("POST", url, headers=headers, data=payload1, verify=False)

    print(x,"\n",response1.text,"\n")

 

 

Script #4 - POST call - Create multiple Zones on one or multiple target Templates

import requests
import urllib3
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

target_templates = ["template1","template2","template3"]

for x in target_templates:

    payload1 = json.dumps({
      "entry": {
        "@name": "{new_zone1}",
        "@location": "template",
        "@template": x,
        "@vsys": "vsys1",
        "network": {
          "layer3": {},
          "log-setting": "default"
        }
      }
    })

    payload2 = json.dumps({
      "entry": {
        "@name": "{new_zone2}",
        "@location": "template",
        "@template": x,
        "@vsys": "vsys1",
        "network": {
          "layer3": {},
          "log-setting": "default"
        }
      }
    })

    payload3 = json.dumps({
      "entry": {
        "@name": "{new_zone3}",
        "@location": "template",
        "@template": x,
        "@vsys": "vsys1",
        "network": {
          "layer3": {},
          "log-setting": "default"
        }
      }
    })


    url1 = ("https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_OS_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1&name={new_zone1}")
    url2 = ("https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_OS_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1&name={new_zone2}")
    url3 = ("https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_OS_VERSION}/Network/Zones?location=template&template="+x+"&vsys=vsys1&name={new_zone3}")
	
    headers = {
      'Content-Type': 'application/json',
      'X-PAN-KEY': '{PANORAMA_API_KEY}'
    }

    response1 = requests.request("POST", url1, headers=headers, data=payload1, verify=False)
    response2 = requests.request("POST", url2, headers=headers, data=payload2, verify=False)
    response3 = requests.request("POST", url3, headers=headers, data=payload3, verify=False)

    print(x,"\n",response1.text,"\n", response2.text,"\n", response3.text,"\n")

 

Please note that the scripts #1,#2 and #3 can also be modified in to manipulate other Template configurations such as Ethernet Interfaces, Aggregate Interfaces, VLANs, etc

 

Script #4 - GET call - List all "Shared" Objects from a Target Device Group

 

import requests
import urllib3
import pprint
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

target_device_group = "{target_device_group_name}"
headers = {
  'Content-Type': 'application/json',
  'X-PAN-KEY': '{PANORAMA_API_KEY}'
}

url = ("https://{PANORAMA_IP_ADDRESS}/restapi/v{PANORAMA_OS_VERSION}/Objects/Addresses?location=shared&device-group="+target_device_group)

response = requests.request("GET", url, headers=headers, verify=False)

response_dict = json.loads(response.text)
print(target_device_group)
pprint.pp(response_dict)

 

Hope it helps as much as it did for me! 🙂

 

 

 

 

 

 

 

 

 

 

 

 

 

0 REPLIES 0
  • 5801 Views
  • 0 replies
  • 5 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!