-
Notifications
You must be signed in to change notification settings - Fork 0
/
pan_net_dns_proxy.py
155 lines (108 loc) · 4.05 KB
/
pan_net_dns_proxy.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
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
#!/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
"""
from pan_data import init_data, write_data
import timeit
verbose, debug = True, False
def pan_net_dns_proxy():
key = 'N_NET_DNS_PROXY'
if key not in cf or cf[key] <= 0:
return
n = cf[key]
print("\nNetwork > DNS Proxy ({0})".format(n), end=" ", flush=True)
t0 = timeit.default_timer()
ti = t0
pre = 'dns'
vsys = cf['VSYS']
data = init_data(pre)
data['dump'].append("<vsys><entry name='{0}'><dns-proxy>".format(vsys))
if 'XPATH_TPL' not in cf:
print("pan_net_dns_proxy: XPATH_TPL not set: Panorama template not specified")
return
x = cf['XPATH_TPL']
lhost = cf['LHOST']
# xpath = "{0}/config/devices/entry[@name='{1}']/vsys/entry[@name='{2}']/dns-proxy".format(x, lhost, vsys)
xpath = "{0}/config/devices/entry[@name='{1}']/network/dns-proxy".format(x, lhost)
data['xml'][0] = data['xml'][0] % xpath
data['clean_xml'][0] = data['clean_xml'][0] % xpath
# static parameters: move them back to the loop if they become dynamic
#
primary = secondary = ""
if 'DNS_PROXY_PRIMARY' in cf and len(cf['DNS_PROXY_PRIMARY']) > 0:
primary = "<primary>{0}</primary>".format(cf['DNS_PROXY_PRIMARY'])
if 'DNS_PROXY_SECONDARY' in cf and len(cf['DNS_PROXY_SECONDARY']) > 0:
secondary = "<secondary>{0}</secondary>".format(cf['DNS_PROXY_SECONDARY'])
entry = {}
entry['default'] = f"""
<default>
{primary}{secondary}
</default>"""
entry['cache'] = """
<cache>
<max-ttl>
<enabled>no</enabled>
</max-ttl>
<enabled>yes</enabled>
</cache>"""
entry['tcp_queries'] = """
<tcp-queries>
<enabled>no</enabled>
</tcp-queries>"""
entry['interface'] = "" # empty now
"""
<interface>
<member>ethernet1/2</member>
<member>ethernet1/3</member>
</interface>"""
static_entries = []
for i in range(1, cf['DNS_PROXY_STATIC_ENTRIES'] + 1):
fqdn = cf['DNS_PROXY_STATIC_FQDN'] % i
address = cf['DNS_PROXY_STATIC_ADDR'] # currently a constant value
static_entries.append(
f"""
<entry name="{fqdn}">
<address>
<member>{address}</member>
</address>
<domain>{fqdn}</domain>
</entry>""")
if len(static_entries) > 0:
entry['static_entries'] = """
<static-entries>{0}
</static-entries>""".format(''.join(static_entries))
entry_details = ''.join(entry.values())
# static variables in the loop
#
s = n // 10 # increment per slice: 10%, 20%, etc..
for i in range(1, n + 1):
dns_proxy_name = cf['DNS_PROXY_NAME'] % i
element = f"""
<entry name=\"{dns_proxy_name}\">{entry_details}
</entry>"""
clean_element = "@name='{0}' or ".format(dns_proxy_name)
data['xml'].append(element)
data['clean_xml'].append(clean_element)
data['dump'].append(element)
time_elapsed = timeit.default_timer() - ti
if time_elapsed > 1:
print('.', end="", flush=True)
ti = timeit.default_timer()
if n > cf['LARGE_N'] and i % s == 0:
print("{:.0%}".format(i / n), end="", flush=True)
data['clean_xml'].append("@name='_z']")
data['dump'].append("</dns-proxy></entry></vsys>")
write_data(data)
print(cf['_msgs']['ok'] % (timeit.default_timer() - t0), end="")
def go():
pan_net_dns_proxy()
if __name__ == '__main__':
cf = {}
go()
else:
from __main__ import cf
verbose = cf['VERBOSE']
debug = cf['DEBUG']