-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
96 lines (75 loc) · 2.57 KB
/
lib.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
#!/usr/bin/env python3
import ujson as json
import netaddr
import ipaddress
import socket
import pytricia
def import_json(filename):
try:
with open(filename, 'r') as fp:
data = json.load(fp)
return data
except Exception:
print('Import Error with file:', filename)
exit()
def export_json(data, filename):
try:
with open(filename, 'w') as fp:
json.dump(data, fp)
except Exception:
print('Export Error with file:', filename)
exit()
def export_pyt_to_json(pyt, filename):
data = {}
try:
with open(filename, 'w') as fp:
for prefix in pyt:
data[prefix] = pyt[prefix]
json.dump(data, fp)
except Exception:
print('Export Error with file:', filename)
def is_valid_ip_address(address, kind):
if not address:
return False
else:
# For IP handling
if kind == 'ip':
try:
socket.inet_aton(address)
return True
except socket.error as e:
print('error with IP:', address, '-', e)
return False
# For Prefix Handling
elif kind == 'prefix':
try:
if ':' not in address and ipaddress.IPv4Network(address):
return True
elif ':' in address and ipaddress.IPv6Network(address):
return True
except ValueError as e:
print('Error 1 with prefix:', address, '-', e)
except ipaddress.AddressValueError as e:
print('Error 2 with prefix:', address, '-', e)
except ipaddress.NetmaskValueError as e:
print('Error 3 with prefix:', address, '-', e)
return False
def calc_prefix_diff(super_prefix, sub_prefix):
super_prefix_netaddr = netaddr.IPNetwork(super_prefix)
sub_prefix_netaddr = netaddr.IPNetwork(sub_prefix)
prefix_diff_list = netaddr.cidr_exclude(super_prefix_netaddr, sub_prefix_netaddr)
return set(map(str, prefix_diff_list))
def dict_list_to_set(data):
for entry in data:
data[entry] = set(data[entry])
return data
def dict_mask_to_prefixes(prefixes):
mask_to_prefixes = {}
for prefix in prefixes:
mask = int(prefix.split('/')[1])
if mask in mask_to_prefixes:
mask_to_prefixes[mask].add(prefix)
else:
mask_to_prefixes[mask] = set()
mask_to_prefixes[mask].add(prefix)
return {mask:mask_to_prefixes[mask] for mask in sorted(mask_to_prefixes)}