<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: adding object to specific device group via panorama and API in Automation/API Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360462#M2493</link>
    <description>&lt;P&gt;There may be a few pieces still in that code that im not using, but since it was my first ever script i wrote, I just left a few items in there with notes what it was for... figured at some point i should get the time to go back and make it more effieienct and actually look for the commit job ID and status it before saying it is done..but again..this works for me 100% of the time to add a single object with a tag that gets sucked into a Dynamic Address group which uses that Tag. You shoul be able to add multiple tags pretty easy with this code as a starter&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Nov 2020 13:55:05 GMT</pubDate>
    <dc:creator>JohnCrubaugh</dc:creator>
    <dc:date>2020-11-03T13:55:05Z</dc:date>
    <item>
      <title>adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276241#M1971</link>
      <description>&lt;DIV class="lia-message-subject lia-component-message-view-widget-subject"&gt;&lt;DIV class="MessageSubject"&gt;&lt;DIV class="MessageSubjectIcons "&gt;&lt;DIV class="lia-message-subject"&gt;Add addressobject to specific device group through panorama via python&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="lia-message-body lia-component-message-view-widget-body lia-component-body-signature-highlight-escalation lia-component-message-view-widget-body-signature-highlight-escalation"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;Ok, i will first start by saying ive never attempted anything like this before. Im an old school network engineer but times are a changing and im trying to learn how to automate simple tasks. All I need to do is promt the user to enter and IP address and then add that IP Address to a specific device group with 1 tag on it.&amp;nbsp; for example i want to add 7.7.7.7 to my firewall with the tag SFTP. So once the object hits the firewall the name would be H-7.7.7.7 and description would simply be the ticket number of the request and the ip would be learned via input and then I would automatically include the tag. ive been reading forums and other items and came up with a little script below.&amp;nbsp; This is what I have so far, but im kinda stuck now. Ive only been messing around with this for a couple of days...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This defines the panorama device interface to connect to and the DeviceGroup&lt;BR /&gt;device = ["x.x.x.x"]&lt;BR /&gt;devicegroup = ["mydevice"]&lt;BR /&gt;auth_key = "&amp;lt;my-key&amp;gt;"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This intitaties the panorama connection&amp;nbsp;&lt;BR /&gt;pano = panorama.Panorama(device, api_key=auth_key)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This defines the device group we wil be connecting to&lt;BR /&gt;panogrp = panorama.DeviceGroup(devicegroup)&lt;BR /&gt;pano.add(panogrp)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This will set the IP address of the SFTP source to add to the Firewall&amp;nbsp;&lt;BR /&gt;ip_address = input('Please enter the ip address you wish to add to the SFTP whitelist, (example x.x.x.x): ')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This will set the description of the node to the Release Ticket number&lt;BR /&gt;description = input("Please enter the release ticket number: ")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#This formats the name to match the existing standards in the firewall&lt;BR /&gt;name = ("H-"+ip_address)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#declares an object to create&lt;/P&gt;&lt;P&gt;sftp_server = pandevice.objects.AddressObject()&lt;BR /&gt;panogrp.add(sftp_server)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any suggestions to make it better and or complete the adding of the node..im not sure im even on the right path so far... thanks for any insight ... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 11 Jul 2019 20:04:35 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276241#M1971</guid>
      <dc:creator>JohnCrubaugh</dc:creator>
      <dc:date>2019-07-11T20:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276340#M1972</link>
      <description>&lt;P&gt;You're on the right path, just a few tweaks:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Both &lt;CODE&gt;device&lt;/CODE&gt; and &lt;CODE&gt;devicegroup&lt;/CODE&gt; should be strings like &lt;CODE&gt;auth_key&lt;/CODE&gt;, not lists&lt;/LI&gt;
&lt;LI&gt;When you create the address object, you can pass in the params that you've collected from the user:
&lt;PRE&gt;sftp_server = pandevice.objects.AddressObject(name=name, value=ip_address, description=description)&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Finally, once you've finished setting up your pandevice object tree, remember to invoke &lt;CODE&gt;create() / apply() / delete()&lt;/CODE&gt; on the object to make the configuration change, depending on if you're trying to create, update, or delete:
&lt;PRE&gt;sftp_server.create()&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 12 Jul 2019 15:34:28 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276340#M1972</guid>
      <dc:creator>gfreeman</dc:creator>
      <dc:date>2019-07-12T15:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276341#M1973</link>
      <description>&lt;P&gt;thank you, i am still learning how this all works, but that makes sense. I apprecaite the guidance. Ill let you know my outcome once i get this going.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my next goal is doing the commit to panorama and the device group push.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You again!!&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2019 15:54:01 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/276341#M1973</guid>
      <dc:creator>JohnCrubaugh</dc:creator>
      <dc:date>2019-07-12T15:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360454#M2490</link>
      <description>&lt;P&gt;How did you ever make out with this?&amp;nbsp; Do you have any running code you care to share?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 13:19:00 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360454#M2490</guid>
      <dc:creator>rnobrega</dc:creator>
      <dc:date>2020-11-03T13:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360460#M2491</link>
      <description>&lt;P&gt;I did i have a script that today will add a address object, tag it, put a ticket number as the description ( for audit purposes ) and then perform both a panorama commit and device group commit.&amp;nbsp; let me pull out any private info and i can post it....&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 13:45:15 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360460#M2491</guid>
      <dc:creator>JohnCrubaugh</dc:creator>
      <dc:date>2020-11-03T13:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360461#M2492</link>
      <description>&lt;P&gt;#!/usr/bin/env python3&lt;/P&gt;&lt;P&gt;import pandevice&lt;BR /&gt;import requests&lt;BR /&gt;import json&lt;BR /&gt;import urllib3&lt;BR /&gt;import time&lt;BR /&gt;import sys&lt;BR /&gt;import ipaddress&lt;BR /&gt;import xml.etree.ElementTree as ET&lt;BR /&gt;from requests.exceptions import HTTPError&lt;BR /&gt;from pandevice import panorama&lt;BR /&gt;from pandevice import objects&lt;BR /&gt;from pandevice import policies&lt;BR /&gt;from pandevice.base import PanObject&lt;BR /&gt;from pandevice.panorama import Panorama&lt;/P&gt;&lt;P&gt;# This ensures you wont see an error about certificate validation for SSL&lt;BR /&gt;verify = False&lt;BR /&gt;if not verify:&lt;BR /&gt;from requests.packages.urllib3.exceptions import InsecureRequestWarning&lt;BR /&gt;requests.packages.urllib3.disable_warnings(InsecureRequestWarning)&lt;/P&gt;&lt;P&gt;# this defines the api calls necessary to perform Palo Alto commits&lt;BR /&gt;# palo alto has a two step commit process. The first step is to commit to panorama&lt;/P&gt;&lt;P&gt;commit_panorama_api = "https://&amp;lt;panorama URL&amp;gt;/api/?type=commit&amp;amp;cmd=&amp;lt;commit&amp;gt;&amp;lt;/commit&amp;gt;&amp;amp;key=&amp;lt;your api key&amp;gt;"&lt;/P&gt;&lt;P&gt;# the second step is to commit the specific device group, in this instance we only plan to commit to Production, so you can commit any enviornment by updating the name production with whatever device group your working wiht.&lt;BR /&gt;commit_production_api = "https://&amp;lt;panorama URL&amp;gt;/api/?type=commit&amp;amp;action=all&amp;amp;cmd=&amp;lt;commit-all&amp;gt;&amp;lt;shared-policy&amp;gt;&amp;lt;device-group&amp;gt;&amp;lt;entry%20name=\"Production\"/&amp;gt;&amp;lt;/device-group&amp;gt;&amp;lt;/shared-policy&amp;gt;&amp;lt;/commit-all&amp;gt;&amp;amp;key=&amp;lt;your api key&amp;gt;"&lt;/P&gt;&lt;P&gt;# this defines the current list of address objects. This will be used to populate the current list of static address&lt;BR /&gt;# objects, so we can check the user entry to verify it will NOT be a duplicate.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;# This defines the panorama device interface to connect to and the DeviceGroup&lt;BR /&gt;device = "x.x.x.x"&lt;BR /&gt;devicegroup = "Production"&lt;BR /&gt;auth_key = "&amp;lt;Your API KEY&amp;gt;"&lt;/P&gt;&lt;P&gt;# This defines how we will connect to panorama&lt;BR /&gt;pano = panorama.Panorama(device, api_key=auth_key)&lt;/P&gt;&lt;P&gt;# This defines the device group we wil be connecting to&lt;BR /&gt;palo_device_group = panorama.DeviceGroup(devicegroup)&lt;BR /&gt;pano.add(palo_device_group)&lt;/P&gt;&lt;P&gt;# This will set the IP address of the SFTP source to add to the Production Firewall DeviceGroup&lt;BR /&gt;ip_address = input('Please enter the ip address you wish to add to the SFTP whitelist, (example: x.x.x.x): ')&lt;/P&gt;&lt;P&gt;#this will validate the user entered a valid IPv4 address or network statement&lt;BR /&gt;try:&lt;BR /&gt;ip_addr = ipaddress.IPv4Network(ip_address)&lt;BR /&gt;except ValueError:&lt;BR /&gt;print("this address is not a valid IPv4 address: ", ip_address)&lt;BR /&gt;sys.exit()&lt;/P&gt;&lt;P&gt;# This will set the description of the node to the Release Ticket number&lt;BR /&gt;description = input('Please enter the release ticket number(just the numbers): ')&lt;/P&gt;&lt;P&gt;# this is the api call to make to get the current list of address objects:&lt;BR /&gt;get_address_objects_api = 'https://&amp;lt;panorama URL&amp;gt;/restapi/9.0/Objects/Addresses?location=device-group&amp;amp;device-group=Production&amp;amp;key=&amp;lt;yourapikey&amp;gt;'&lt;/P&gt;&lt;P&gt;try:&lt;BR /&gt;current_address_objects_response = requests.get(get_address_objects_api, verify=False)&lt;BR /&gt;# if the response was successful, no Exception will be raised&lt;BR /&gt;current_address_objects_response.raise_for_status()&lt;BR /&gt;except HTTPError as http_err:&lt;BR /&gt;print(f'HTTP error occurred: {http_err}')&lt;BR /&gt;except Exception as err:&lt;BR /&gt;print(f'Other error occurred: {err}')&lt;BR /&gt;else:&lt;BR /&gt;print("Please wait while your Address Object query processes!")&lt;BR /&gt;time.sleep(5)&lt;BR /&gt;address_todos_dict = json.loads(current_address_objects_response.text)&lt;/P&gt;&lt;P&gt;# establish the length of the dictionary to loop through&lt;BR /&gt;i = 0&lt;BR /&gt;length = len(address_todos_dict['result']['entry'])&lt;BR /&gt;#validate that the value i am using to key on exists in the data set, if it does execute the lookups and validation&lt;BR /&gt;#to see if the IP address is already in the SFTP list.&lt;BR /&gt;while i &amp;lt; length:&lt;BR /&gt;k = 'ip-netmask'&lt;BR /&gt;if k in address_todos_dict['result']['entry'][i].keys():&lt;BR /&gt;temp_ip = address_todos_dict['result']['entry'][i]['ip-netmask']&lt;BR /&gt;if temp_ip == ip_address:&lt;BR /&gt;print("Your address already exists in the SFTP whitelist")&lt;BR /&gt;i = length&lt;BR /&gt;sys.exit()&lt;BR /&gt;i += 1&lt;/P&gt;&lt;P&gt;# This formats the name to match the existing standards in the firewall&lt;BR /&gt;name = ("N-" + ip_address)&lt;BR /&gt;full_description = ("NETENG-" + description)&lt;/P&gt;&lt;P&gt;# declares an address object to create with the tag SFTP which will automatically put it in the dynamic addrss group&lt;BR /&gt;# for SFTP services&lt;/P&gt;&lt;P&gt;sftp_server = pandevice.objects.AddressObject(name=name, value=ip_address, description=full_description, tag="SFTP")&lt;BR /&gt;palo_device_group.add(sftp_server)&lt;BR /&gt;sftp_server.create()&lt;/P&gt;&lt;P&gt;# this next piece will commit the change to panorama&lt;BR /&gt;try:&lt;BR /&gt;panorama_commit_response = requests.get(commit_panorama_api, verify=False)&lt;BR /&gt;# if the response was successful, no Exception will be raised&lt;BR /&gt;panorama_commit_response.raise_for_status()&lt;BR /&gt;except HTTPError as http_err:&lt;BR /&gt;print(f'HTTP error occurred: {http_err}')&lt;BR /&gt;except Exception as err:&lt;BR /&gt;print(f'Other error occurred: {err}')&lt;BR /&gt;else:&lt;BR /&gt;print("Please wait while your panorama commit processes!")&lt;BR /&gt;time.sleep(30)&lt;BR /&gt;print('Your panorama commit was successful')&lt;/P&gt;&lt;P&gt;# this piece will do the device commit to the production device group&lt;BR /&gt;try:&lt;BR /&gt;device_commit_response = requests.get(commit_production_api, verify=False)&lt;BR /&gt;# if the response was successful, no Exception will be raised&lt;BR /&gt;device_commit_response.raise_for_status()&lt;BR /&gt;except HTTPError as http_err:&lt;BR /&gt;print(f'HTTP error occurred: {http_err}')&lt;BR /&gt;except Exception as err:&lt;BR /&gt;print(f'Other error occurred: {err}')&lt;BR /&gt;else:&lt;BR /&gt;print("Please wait while the Production Device group commits!")### i should be checking panorama for job status but i didnt know how when i write this, so i just did a sleep ####&lt;BR /&gt;time.sleep(60)&lt;BR /&gt;print('Your IP address was successfully added to the Production Firewall and the Device commit was successful')&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 13:50:08 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360461#M2492</guid>
      <dc:creator>JohnCrubaugh</dc:creator>
      <dc:date>2020-11-03T13:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: adding object to specific device group via panorama and API</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360462#M2493</link>
      <description>&lt;P&gt;There may be a few pieces still in that code that im not using, but since it was my first ever script i wrote, I just left a few items in there with notes what it was for... figured at some point i should get the time to go back and make it more effieienct and actually look for the commit job ID and status it before saying it is done..but again..this works for me 100% of the time to add a single object with a tag that gets sucked into a Dynamic Address group which uses that Tag. You shoul be able to add multiple tags pretty easy with this code as a starter&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 13:55:05 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/adding-object-to-specific-device-group-via-panorama-and-api/m-p/360462#M2493</guid>
      <dc:creator>JohnCrubaugh</dc:creator>
      <dc:date>2020-11-03T13:55:05Z</dc:date>
    </item>
  </channel>
</rss>

