forked from jubopako/jinja2-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetaddr_filters.py
521 lines (409 loc) · 13.9 KB
/
netaddr_filters.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# From netadd-filters.py (c) 2014, Maciej Delmanowski <[email protected]>
#
# add netaddr filters to jinja2-live parser
try:
import netaddr
except:
raise ValueError('python-netaddr package is not installed')
# ---- IP address and network filters ----
def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
""" Check if string is an IP address or network and filter it """
query_types = [ 'type', 'bool', 'int', 'version', 'size', 'address', 'ip', 'host', \
'network', 'subnet', 'prefix', 'broadcast', 'netmask', 'hostmask', \
'unicast', 'multicast', 'private', 'public', 'loopback', 'lo', \
'revdns', 'wrap', 'ipv6', 'v6', 'ipv4', 'v4', 'cidr', 'net', \
'hostnet', 'router', 'gateway', 'gw', 'host/prefix', 'address/prefix' ]
if not value:
return False
elif value == True:
return False
# Check if value is a list and parse each element
elif isinstance(value, (list, tuple)):
_ret = []
for element in value:
if ipaddr(element, str(query), version):
_ret.append(ipaddr(element, str(query), version))
if _ret:
return _ret
else:
return list()
# Check if value is a number and convert it to an IP address
elif str(value).isdigit():
# We don't know what IP version to assume, so let's check IPv4 first,
# then IPv6
try:
if ((not version) or (version and version == 4)):
v = netaddr.IPNetwork('0.0.0.0/0')
v.value = int(value)
v.prefixlen = 32
elif version and version == 6:
v = netaddr.IPNetwork('::/0')
v.value = int(value)
v.prefixlen = 128
# IPv4 didn't work the first time, so it definitely has to be IPv6
except:
try:
v = netaddr.IPNetwork('::/0')
v.value = int(value)
v.prefixlen = 128
# The value is too big for IPv6. Are you a nanobot?
except:
return False
# We got an IP address, let's mark it as such
value = str(v)
vtype = 'address'
# value has not been recognized, check if it's a valid IP string
else:
try:
v = netaddr.IPNetwork(value)
# value is a valid IP string, check if user specified
# CIDR prefix or just an IP address, this will indicate default
# output format
try:
address, prefix = value.split('/')
vtype = 'network'
except:
vtype = 'address'
# value hasn't been recognized, maybe it's a numerical CIDR?
except:
try:
address, prefix = value.split('/')
address.isdigit()
address = int(address)
prefix.isdigit()
prefix = int(prefix)
# It's not numerical CIDR, give up
except:
return False
# It is something, so let's try and build a CIDR from the parts
try:
v = netaddr.IPNetwork('0.0.0.0/0')
v.value = address
v.prefixlen = prefix
# It's not a valid IPv4 CIDR
except:
try:
v = netaddr.IPNetwork('::/0')
v.value = address
v.prefixlen = prefix
# It's not a valid IPv6 CIDR. Give up.
except:
return False
# We have a valid CIDR, so let's write it in correct format
value = str(v)
vtype = 'network'
v_ip = netaddr.IPAddress(str(v.ip))
# We have a query string but it's not in the known query types. Check if
# that string is a valid subnet, if so, we can check later if given IP
# address/network is inside that specific subnet
try:
if query and query not in query_types and ipaddr(query, 'network'):
iplist = netaddr.IPSet([netaddr.IPNetwork(query)])
query = 'cidr_lookup'
except:
None
# This code checks if value maches the IP version the user wants, ie. if
# it's any version ("ipaddr()"), IPv4 ("ipv4()") or IPv6 ("ipv6()")
# If version does not match, return False
if version and v.version != version:
return False
# We don't have any query to process, so just check what type the user
# expects, and return the IP address in a correct format
if not query:
if v:
if vtype == 'address':
return str(v.ip)
elif vtype == 'network':
return str(v)
elif query == 'type':
if v.size == 1:
return 'address'
if v.size > 1:
if v.ip != v.network:
return 'address'
else:
return 'network'
elif query == 'bool':
if v:
return True
elif query == 'int':
if vtype == 'address':
return int(v.ip)
elif vtype == 'network':
return str(int(v.ip)) + '/' + str(int(v.prefixlen))
elif query == 'version':
return v.version
elif query == 'size':
return v.size
elif query in [ 'address', 'ip' ]:
if v.size == 1:
return str(v.ip)
if v.size > 1:
if v.ip != v.network:
return str(v.ip)
elif query == 'host':
if v.size == 1:
return str(v)
elif v.size > 1:
if v.ip != v.network:
return str(v.ip) + '/' + str(v.prefixlen)
elif query == 'net':
if v.size > 1:
if v.ip == v.network:
return str(v.network) + '/' + str(v.prefixlen)
elif query in [ 'hostnet', 'router', 'gateway', 'gw', 'host/prefix', 'address/prefix' ]:
if v.size > 1:
if v.ip != v.network:
return str(v.ip) + '/' + str(v.prefixlen)
elif query == 'network':
if v.size > 1:
return str(v.network)
elif query == 'subnet':
return str(v.cidr)
elif query == 'cidr':
return str(v)
elif query == 'prefix':
return int(v.prefixlen)
elif query == 'broadcast':
if v.size > 1:
return str(v.broadcast)
elif query == 'netmask':
if v.size > 1:
return str(v.netmask)
elif query == 'hostmask':
return str(v.hostmask)
elif query == 'unicast':
if v.is_unicast():
return value
elif query == 'multicast':
if v.is_multicast():
return value
elif query == 'link-local':
if v.version == 4:
if ipaddr(str(v_ip), '169.254.0.0/24'):
return value
elif v.version == 6:
if ipaddr(str(v_ip), 'fe80::/10'):
return value
elif query == 'private':
if v.is_private():
return value
elif query == 'public':
if v_ip.is_unicast() and not v_ip.is_private() and \
not v_ip.is_loopback() and not v_ip.is_netmask() and \
not v_ip.is_hostmask():
return value
elif query in [ 'loopback', 'lo' ]:
if v_ip.is_loopback():
return value
elif query == 'revdns':
return v_ip.reverse_dns
elif query == 'wrap':
if v.version == 6:
if vtype == 'address':
return '[' + str(v.ip) + ']'
elif vtype == 'network':
return '[' + str(v.ip) + ']/' + str(v.prefixlen)
else:
return value
elif query in [ 'ipv6', 'v6' ]:
if v.version == 4:
return str(v.ipv6())
else:
return value
elif query in [ 'ipv4', 'v4' ]:
if v.version == 6:
try:
return str(v.ipv4())
except:
return False
else:
return value
elif query == '6to4':
if v.version == 4:
if v.size == 1:
ipconv = str(v.ip)
elif v.size > 1:
if v.ip != v.network:
ipconv = str(v.ip)
else:
ipconv = False
if ipaddr(ipconv, 'public'):
numbers = list(map(int, ipconv.split('.')))
try:
return '2002:{:02x}{:02x}:{:02x}{:02x}::1/48'.format(*numbers)
except:
return False
elif v.version == 6:
if vtype == 'address':
if ipaddr(str(v), '2002::/16'):
return value
elif vtype == 'network':
if v.ip != v.network:
if ipaddr(str(v.ip), '2002::/16'):
return value
else:
return False
elif query == 'cidr_lookup':
try:
if v in iplist:
return value
except:
return False
else:
try:
float(query)
if v.size == 1:
if vtype == 'address':
return str(v.ip)
elif vtype == 'network':
return str(v)
elif v.size > 1:
try:
return str(v[query]) + '/' + str(v.prefixlen)
except:
return False
else:
return value
except:
raise ValueError(alias + ': unknown filter type: %s' % query)
return False
def ipwrap(value, query = ''):
""" wrap ipv6 addresses into brackets """
try:
if isinstance(value, (list, tuple)):
_ret = []
for element in value:
if ipaddr(element, query, version = False, alias = 'ipwrap'):
_ret.append(ipaddr(element, 'wrap'))
else:
_ret.append(element)
return _ret
else:
_ret = ipaddr(value, query, version = False, alias = 'ipwrap')
if _ret:
return ipaddr(_ret, 'wrap')
else:
return value
except:
return value
def ipv4(value, query = ''):
""" alias for ipaddr('ipv4') """
return ipaddr(value, query, version = 4, alias = 'ipv4')
def ipv6(value, query = ''):
""" alias for ipaddr('ipv6') """
return ipaddr(value, query, version = 6, alias = 'ipv6')
# Split given subnet into smaller subnets or find out the biggest subnet of
# a given IP address with given CIDR prefix
# Usage:
#
# - address or address/prefix | ipsubnet
# returns CIDR subnet of a given input
#
# - address/prefix | ipsubnet(cidr)
# returns number of possible subnets for given CIDR prefix
#
# - address/prefix | ipsubnet(cidr, index)
# returns new subnet with given CIDR prefix
#
# - address | ipsubnet(cidr)
# returns biggest subnet with given CIDR prefix that address belongs to
#
# - address | ipsubnet(cidr, index)
# returns next indexed subnet which contains given address
def ipsubnet(value, query = '', index = 'x'):
""" Manipulate IPv4/IPv6 subnets """
try:
vtype = ipaddr(value, 'type')
if vtype == 'address':
v = ipaddr(value, 'cidr')
elif vtype == 'network':
v = ipaddr(value, 'subnet')
value = netaddr.IPNetwork(v)
except:
return False
if not query:
return str(value)
elif str(query).isdigit():
vsize = ipaddr(v, 'size')
query = int(query)
try:
float(index)
index = int(index)
if vsize > 1:
try:
return str(list(value.subnet(query))[index])
except:
return False
elif vsize == 1:
try:
return str(value.supernet(query)[index])
except:
return False
except:
if vsize > 1:
try:
return str(len(list(value.subnet(query))))
except:
return False
elif vsize == 1:
try:
return str(value.supernet(query)[0])
except:
return False
return False
# ---- HWaddr / MAC address filters ----
def hwaddr(value, query = '', alias = 'hwaddr'):
''' Check if string is a HW/MAC address and filter it '''
try:
v = netaddr.EUI(value)
except:
if query and query not in [ 'bool' ]:
raise ValueError(alias + ': not a hardware address: %s' % value)
if not query:
if v:
return value
elif query == 'bool':
if v:
return True
elif query in [ 'win', 'eui48' ]:
v.dialect = netaddr.mac_eui48
return str(v)
elif query == 'unix':
v.dialect = netaddr.mac_unix
return str(v)
elif query in [ 'pgsql', 'postgresql', 'psql' ]:
v.dialect = netaddr.mac_pgsql
return str(v)
elif query == 'cisco':
v.dialect = netaddr.mac_cisco
return str(v)
elif query == 'bare':
v.dialect = netaddr.mac_bare
return str(v)
elif query == 'linux':
v.dialect = mac_linux
return str(v)
else:
raise ValueError(alias + ': unknown filter type: %s' % query)
return False
class mac_linux(netaddr.mac_unix): pass
mac_linux.word_fmt = '%.2x'
def macaddr(value, query = ''):
""" convert MAC address to various fromat """
return hwaddr(value, query, alias = 'macaddr')
# ---- Ansible filters ----
class FilterModule(object):
''' IP address and network manipulation filters '''
def filters(self):
return {
# IP addresses and networks
'ipaddr': ipaddr,
'ipwrap': ipwrap,
'ipv4': ipv4,
'ipv6': ipv6,
'ipsubnet': ipsubnet,
# MAC / HW addresses
'hwaddr': hwaddr,
'macaddr': macaddr
}