-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpan_net_if_tun.py
132 lines (91 loc) · 3.57 KB
/
pan_net_if_tun.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
#!/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_if_tun():
key = 'N_NET_IF_TUNNEL'
if key not in cf or cf[key] <= 0:
return
n = cf[key]
print("\nNetwork > Interfaces > Tunnel ({0}) with zone and VR assigned".format(n), end=" ", flush=True)
t0 = timeit.default_timer()
ti = t0
associations = ('vsys', 'zone', 'vr')
data = init_data('tun', associations)
data['dump'].append("<tunnel>")
if 'XPATH_TPL' not in cf:
print("pan_net_if_tun: XPATH_TPL not set: Panorama template not specified")
return
x = cf['XPATH_TPL']
lhost = cf['LHOST']
vsys = cf['VSYS']
zone = cf['IF_TUNNEL_ZONE']
vr = cf['IF_TUNNEL_VR']
xpath = "{0}/config/devices/entry[@name='{1}']" \
"/network/interface/tunnel/units".format(x, lhost)
xpath_vsys = "{0}/config/devices/entry[@name='{1}']" \
"/vsys/entry[@name='{2}']/import/network/interface".format(x, lhost, vsys)
xpath_zone = "{0}/config/devices/entry[@name='{1}']" \
"/vsys/entry[@name='{2}']/zone/entry[@name='{3}']/network/layer3".format(x, lhost, vsys, zone)
xpath_vr = "{0}/config/devices/entry[@name='{1}']" \
"/network/virtual-router/entry[@name='{2}']/interface".format(x, lhost, vr)
for asso in ("",) + associations:
a = "" if asso == "" else "_" + asso
x = eval('xpath'+a)
data['xml'+a][0] = data['xml'+a][0] % x
data['clean_xml'+a][0] = data['clean_xml'+a][0] % x
ip_octet_i = cf['IF_TUNNEL_IP_OCTET_i']
ip_octet_j = cf['IF_TUNNEL_IP_OCTET_j']
# static variables in the loop
#
s = n // 10 # increment per slice: 10%, 20%, etc..
for if_i in range(n):
if ip_octet_j > 255:
ip_octet_j = 0
ip_octet_i += 1
if_name = "tunnel.{0}".format(if_i + cf['IF_TUNNEL_NAME_i'])
if 'IF_TUNNEL_IP' in cf \
and cf['IF_TUNNEL_IP'] is not None \
and len(cf['IF_TUNNEL_IP']) > 0:
if_ip = cf['IF_TUNNEL_IP'].format(ip_octet_i, ip_octet_j)
ip_octet_j += 1
element = f"<entry name='{if_name}'><ip><entry name='{if_ip}'/></ip></entry>"
else:
element = f"<entry name='{if_name}'/>"
clean_element = "@name='{0}' or ".format(if_name)
data['xml'].append(element)
data['clean_xml'].append(clean_element)
data['dump'].append(element)
element = "<member>{0}</member>".format(if_name)
clean_element = "text()='{0}' or ".format(if_name)
for asso in associations:
data['xml_' + asso].append(element)
data['clean_xml_' + asso].append(clean_element)
time_elapsed = timeit.default_timer() - ti
if time_elapsed > 1:
print('.', end="", flush=True)
ti = timeit.default_timer()
count = if_i + 1
if n > cf['LARGE_N'] and count % s == 0:
print("{:.0%}".format(count / n), end="", flush=True)
data['clean_xml'].append("@name='_z']")
for asso in associations:
data['clean_xml_' + asso].append("text()='_z']")
data['dump'].append("</tunnel>")
write_data(data)
print(cf['_msgs']['ok'] % (timeit.default_timer() - t0), end="")
def go():
pan_net_if_tun()
if __name__ == '__main__':
cf = {}
go()
else:
from __main__ import cf
verbose = cf['VERBOSE']
debug = cf['DEBUG']