77#
88# Firewall admin with superuser and IP address is required.
99#
10- # REQURED :
10+ # REQUIRED :
1111# export PANOS_HOST=""
1212# export PANOS_USER="" #User *MUST* have Commit and Import Permissions in XML API for Admin Role
1313# export PANOS_PASS=""
1414#
1515# OPTIONAL
16- # export PANOS_TEMPLATE="" #Template Name of panorama managed devices
16+ # export PANOS_TEMPLATE="" # Template Name of panorama managed devices
17+ # export PANOS_TEMPLATE_STACK="" # set a Template Stack if certificate should also be pushed automatically
18+ # export PANOS_VSYS="Shared" # name of the vsys to import the certificate
1719#
1820# The script will automatically generate a new API key if
1921# no key is found, or if a saved key has expired or is invalid.
2022
23+
24+
25+
2126# This function is to parse the XML response from the firewall
2227parse_response () {
2328 type=$2
29+ _debug " API Response: $1 "
2430 if [ " $type " = ' keygen' ]; then
2531 status=$( echo " $1 " | sed ' s/^.*\([' \' ' ]\)\([a-z]*\)' \' ' .*/\2/g' )
2632 if [ " $status " = " success" ]; then
@@ -30,6 +36,13 @@ parse_response() {
3036 message=" PAN-OS Key could not be set."
3137 fi
3238 else
39+ if [ " $type " = ' commit' ]; then
40+ job_id=$( echo " $1 " | sed ' s/^.*\(<job>\)\(.*\)<\/job>.*/\2/g' )
41+ _commit_job_id=$job_id
42+ elif [ " $type " = ' job_status' ]; then
43+ job_status=$( echo " $1 " | tr -d ' \n' | sed ' s/^.*<result>\([^<]*\)<\/result>.*/\1/g' )
44+ _commit_job_status=$job_status
45+ fi
3346 status=$( echo " $1 " | tr -d ' \n' | sed ' s/^.*"\([a-z]*\)".*/\1/g' )
3447 message=$( echo " $1 " | tr -d ' \n' | sed ' s/.*\(<result>\|<msg>\|<line>\)\([^<]*\).*/\2/g' )
3548 _debug " Firewall message: $message "
@@ -44,7 +57,7 @@ parse_response() {
4457# This function is used to deploy to the firewall
4558deployer () {
4659 content=" "
47- type=$1 # Types are keytest, keygen, cert, key, commit
60+ type=$1 # Types are keytest, keygen, cert, key, commit, job_status, push
4861 panos_url=" https://$_panos_host /api/"
4962
5063 # Test API Key by performing a lookup
@@ -84,6 +97,9 @@ deployer() {
8497 if [ " $_panos_template " ]; then
8598 content=" $content ${nl} --$delim ${nl} Content-Disposition: form-data; name=\" target-tpl\" \r\n\r\n$_panos_template "
8699 fi
100+ if [ " $_panos_vsys " ]; then
101+ content=" $content ${nl} --$delim ${nl} Content-Disposition: form-data; name=\" target-tpl-vsys\" \r\n\r\n$_panos_vsys "
102+ fi
87103 fi
88104 if [ " $type " = ' key' ]; then
89105 panos_url=" ${panos_url} ?type=import"
@@ -96,6 +112,9 @@ deployer() {
96112 if [ " $_panos_template " ]; then
97113 content=" $content ${nl} --$delim ${nl} Content-Disposition: form-data; name=\" target-tpl\" \r\n\r\n$_panos_template "
98114 fi
115+ if [ " $_panos_vsys " ]; then
116+ content=" $content ${nl} --$delim ${nl} Content-Disposition: form-data; name=\" target-tpl-vsys\" \r\n\r\n$_panos_vsys "
117+ fi
99118 fi
100119 # Close multipart
101120 content=" $content ${nl} --$delim --${nl}${nl} "
@@ -118,6 +137,22 @@ deployer() {
118137 content=" type=commit&action=partial&key=$_panos_key &cmd=$cmd "
119138 fi
120139
140+ # Query job status
141+ if [ " $type " = ' job_status' ]; then
142+ echo " **** Querying job $_commit_job_id status ****"
143+ H1=" Content-Type: application/x-www-form-urlencoded"
144+ cmd=$( printf " %s" " <show><jobs><id>$_commit_job_id </id></jobs></show>" | _url_encode)
145+ content=" type=op&key=$_panos_key &cmd=$cmd "
146+ fi
147+
148+ # Push changes
149+ if [ " $type " = ' push' ]; then
150+ echo " **** Pushing changes ****"
151+ H1=" Content-Type: application/x-www-form-urlencoded"
152+ cmd=$( printf " %s" " <commit-all><template-stack><name>$_panos_template_stack </name><admin><member>$_panos_user </member></admin></template-stack></commit-all>" | _url_encode)
153+ content=" type=commit&action=all&key=$_panos_key &cmd=$cmd "
154+ fi
155+
121156 response=$( _post " $content " " $panos_url " " " " POST" )
122157 parse_response " $response " " $type "
123158 # Saving response to variables
@@ -126,6 +161,8 @@ deployer() {
126161 if [ " $response_status " = " success" ]; then
127162 _debug " Successfully deployed $type "
128163 return 0
164+ elif [ " $_commit_job_status " ]; then
165+ _debug " Commit Job Status = $_commit_job_status "
129166 else
130167 _err " Deploy of type $type failed. Try deploying with --debug to troubleshoot."
131168 _debug " $message "
@@ -191,11 +228,31 @@ panos_deploy() {
191228 _getdeployconf PANOS_TEMPLATE
192229 fi
193230
231+ # PANOS_TEMPLATE_STACK
232+ if [ " $PANOS_TEMPLATE_STACK " ]; then
233+ _debug " Detected ENV variable PANOS_TEMPLATE_STACK. Saving to file."
234+ _savedeployconf PANOS_TEMPLATE_STACK " $PANOS_TEMPLATE_STACK " 1
235+ else
236+ _debug " Attempting to load variable PANOS_TEMPLATE_STACK from file."
237+ _getdeployconf PANOS_TEMPLATE_STACK
238+ fi
239+
240+ # PANOS_TEMPLATE_STACK
241+ if [ " $PANOS_VSYS " ]; then
242+ _debug " Detected ENV variable PANOS_VSYS. Saving to file."
243+ _savedeployconf PANOS_VSYS " $PANOS_VSYS " 1
244+ else
245+ _debug " Attempting to load variable PANOS_VSYS from file."
246+ _getdeployconf PANOS_VSYS
247+ fi
248+
194249 # Store variables
195250 _panos_host=$PANOS_HOST
196251 _panos_user=$PANOS_USER
197252 _panos_pass=$PANOS_PASS
198253 _panos_template=$PANOS_TEMPLATE
254+ _panos_template_stack=$PANOS_TEMPLATE_STACK
255+ _panos_vsys=$PANOS_VSYS
199256
200257 # Test API Key if found. If the key is invalid, the variable _panos_key will be unset.
201258 if [ " $_panos_host " ] && [ " $_panos_key " ]; then
@@ -229,6 +286,18 @@ panos_deploy() {
229286 deployer cert
230287 deployer key
231288 deployer commit
289+ if [ " $_panos_template_stack " ]; then
290+ # try to get job status for 20 times in 30 sec interval
291+ for (( i = 0 ; i < 20 ; i++ )) ; do
292+ deployer job_status
293+ if [[ " $_commit_job_status " == " OK" ]]; then
294+ echo " Commit finished!"
295+ break
296+ fi
297+ sleep 30
298+ done
299+ deployer push
300+ fi
232301 fi
233302 fi
234303}
0 commit comments