-
Notifications
You must be signed in to change notification settings - Fork 0
/
pan_api.py
64 lines (45 loc) · 1.63 KB
/
pan_api.py
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
#!/usr/bin/python3
"""
pan-os_api v2.2 [20230717]
Scripts to generate PA/Panorama config
by Terence LEE <telee.hk@gmail.com>
Details at https://github.com/telee0/pan-os_api.py.git
"""
import requests
import xml.etree.ElementTree as xml
verbose, debug = True, False
def pan_api(access):
requests.packages.urllib3.disable_warnings()
host, user, password = access
url = "https://{0}/api/?type=keygen".format(host)
try:
response = requests.post(url, data={'user': user, 'password': password}, verify=False)
if debug:
print("pan_api: response: ", response.text)
except Exception as e:
if verbose:
print("pan_api: {0}: {1}".format(host, e))
return None
result = xml.fromstring(response.content)
api_key = result.find('result/key')
return api_key.text if api_key is not None else None
def go():
for pa in ['PA1', 'PA2']:
if pa in cf:
desc = ""
if 'DESC' in cf[pa] and len(cf[pa]['DESC']) > 0:
desc = " ({0})".format(cf[pa]['DESC'])
print("{0} = {1}{2}".format(pa, cf[pa]['HOST'], desc))
api_key = pan_api([cf[pa]['HOST'], cf[pa]['USER'], cf[pa]['PASS']])
if api_key is not None and len(api_key) > 0:
cf[pa]['KEY'] = api_key
cf[pa]['URL'] = "https://{0}/api".format(cf[pa]['HOST'])
else:
print("{0}: API key not set. Please check {1} in {2}.".format(cf[pa]['HOST'], pa, cf['CF_PATH']))
if __name__ == '__main__':
cf = {}
go()
else:
from __main__ import cf
verbose = cf['VERBOSE']
debug = cf['DEBUG']