-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPAN-Rule-Shadow.sh
executable file
·184 lines (155 loc) · 4.45 KB
/
PAN-Rule-Shadow.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# AUTHOR: Engin YUCE <enginy88@gmail.com>
# DESCRIPTION: Shell script for fetching rule shadow warning messages on PANOS. (Only works with v9.1+)
# VERSION: 1.0
# LICENSE: Copyright 2020 Engin YUCE. Licensed under the Apache License, Version 2.0.
PAN_USERNAME="admin"
PAN_PASSWORD="admin"
PAN_IP="1.2.3.4"
PAN_VSYS="vsys1"
# BELOW THIS LINE, THERE BE DRAGONS!
_checkVariables()
{
[[ ! -z "$PAN_USERNAME" ]] || { echo "Script variable is missing, exiting!" ; exit 1 ; }
[[ ! -z "$PAN_PASSWORD" ]] || { echo "Script variable is missing, exiting!" ; exit 1 ; }
[[ ! -z "$PAN_IP" ]] || { echo "Script variable is missing, exiting!" ; exit 1 ; }
[[ ! -z "$PAN_VSYS" ]] || { echo "Script variable is missing, exiting!" ; exit 1 ; }
}
_checkCurlAvailable()
{
curl --version &>/dev/null
if [[ $? != 0 ]]
then
echo "Error on finding curl, install the curl utility, exiting!" ; exit 1
fi
}
_getAPIKey()
{
local CALL=$(curl -X GET --insecure -m 5 "https://$PAN_IP/api/?type=keygen&user=$PAN_USERNAME&password=$PAN_PASSWORD" 2>/dev/null)
if [[ $? != 0 || -z "$CALL" ]]
then
echo "Error on curl call, check the IP, exiting!" ; exit 1
fi
echo "$CALL" | grep -F "response" | grep -F "status" | grep -F "success" &>/dev/null
if [[ $? != 0 ]]
then
echo "Error on curl response, check the PAN credentials, exiting!" ; exit 1
fi
KEY=$(echo "$CALL" | sed -n 's/.*<key>\([a-zA-Z0-9=]*\)<\/key>.*/\1/p')
if [[ $? != 0 || X"$KEY" == X"" ]]
then
echo "Error on curl response, cannot parse API key, exiting!" ; exit 1
fi
}
_callXMLAPIShadowCount()
{
local CALL=$(curl -H "X-PAN-KEY: $KEY" --insecure -m 5 "https://$PAN_IP/api/?type=op&cmd=<show><shadow-warning><count><vsys>$PAN_VSYS</vsys></count></shadow-warning></show>" 2>/dev/null)
if [[ $? != 0 || -z "$CALL" ]]
then
echo "Error on curl call, check the IP, exiting!" ; exit 1
fi
echo "$CALL" | grep -F "response" | grep -F "status" | grep -F "success" &>/dev/null
if [[ $? != 0 ]]
then
echo "Error on curl response, check the PANOS version, exiting!" ; exit 1
fi
COUNT_RESPONSE=$CALL
}
_callXMLAPIShadowWarning()
{
local CALL=$(curl -H "X-PAN-KEY: $KEY" --insecure -m 5 "https://$PAN_IP/api/?type=op&cmd=<show><shadow-warning><warning-message><vsys>$PAN_VSYS</vsys><uuid>$1</uuid></warning-message></shadow-warning></show>" 2>/dev/null)
if [[ $? != 0 || -z "$CALL" ]]
then
echo "Error on curl call, check the IP, exiting!" ; exit 1
fi
echo "$CALL" | grep -F "response" | grep -F "status" | grep -F "success" &>/dev/null
if [[ $? != 0 ]]
then
echo "Error on curl response, check the PANOS version, exiting!" ; exit 1
fi
WARNING_RESPONSE=$CALL
}
_iterateShadowCount()
{
while :
do
local ENTRY_SEPERATOR="<entry name="
case $COUNT_RESPONSE in
(*"$ENTRY_SEPERATOR"*)
local FOUND="y"
local BEFORE=${COUNT_RESPONSE%%"$ENTRY_SEPERATOR"*}
local AFTER=${COUNT_RESPONSE#*"$ENTRY_SEPERATOR"}
;;
(*)
local FOUND="n"
local BEFORE=$COUNT_RESPONSE
local AFTER=
;;
esac
if [[ x$FOUND == xy ]]
then
local UUID_SEPERATOR="uuid="
local UUID_STRING=${AFTER#*"$UUID_SEPERATOR"}
local UUID_STRING=$(echo $UUID_STRING | head)
local UUID=$(echo $UUID_STRING | cut -d \" -f 2)
UUID_ARRAY+=("$UUID")
COUNT_RESPONSE=$AFTER
else
break
fi
done
}
_iterateShadowWarning()
{
while :
do
local MEMBER_SEPERATOR="<member>"
case $WARNING_RESPONSE in
(*"$MEMBER_SEPERATOR"*)
local FOUND="y"
local BEFORE=${WARNING_RESPONSE%%"$MEMBER_SEPERATOR"*}
local AFTER=${WARNING_RESPONSE#*"$MEMBER_SEPERATOR"}
;;
(*)
local FOUND="n"
local BEFORE=$WARNING_RESPONSE
local AFTER=
;;
esac
if [[ x$FOUND == xy ]]
then
local MEMBER_CLOSE_SEPERATOR="</member>"
local MESSAGE_STRING_BEFORE=${AFTER%%"$MEMBER_CLOSE_SEPERATOR"*}
local MESSAGE_STRING_AFTER=${AFTER#*"$MEMBER_CLOSE_SEPERATOR"}
local MESSAGE_STRING=$(echo $MESSAGE_STRING_BEFORE | head)
MESSAGE_ARRAY+=("$MESSAGE_STRING")
WARNING_RESPONSE=$MESSAGE_STRING_AFTER
else
break
fi
done
}
_main()
{
_checkVariables
_checkCurlAvailable
echo "Attempt to fetch rule shadow warnings. (TIME: $(date))"
echo "Using IP: $PAN_IP, USER: $PAN_USERNAME, VSYS: $PAN_VSYS."
echo "---"
_getAPIKey
_callXMLAPIShadowCount
_iterateShadowCount
for VALUE in "${UUID_ARRAY[@]}"
do
_callXMLAPIShadowWarning $VALUE
_iterateShadowWarning
for VALUE in "${MESSAGE_ARRAY[@]}"
do
echo $VALUE
done
unset MESSAGE_ARRAY
done
echo "---"
echo "All succeeded, bye!"
}
_main