-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexclude-addresses.py
executable file
·212 lines (164 loc) · 6.68 KB
/
exclude-addresses.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
#!/usr/bin/env python3
import sys
from argparse import ArgumentParser, Namespace
from typing import Iterator, List, Optional, NoReturn, Union
from ipaddress import (
IPv4Network, IPv6Network,
ip_address, ip_network,
collapse_addresses
)
ARGHELP_NETWORK = "The network from which we exclude addresses"
ARGHELP_ADDRESSES = "comma or whitespace separated addresses of hosts and/or networks to be excluded"
ARGHELP_SEPARATOR = "separator for the list of resulting networks. Default is the new line"
ARGHELP_PREFIX = "prefix to put before each resulting network, for example `ip route add `"
ARGHELP_POSTFIX = "postfix to be placed after each resulting network, for example ` via tun0`"
ARGHELP_IGNORE = "ignore non-valid input arguments (except the target network)"
def parse_arguments() -> Namespace:
parser = ArgumentParser()
parser.add_argument('network', type=str, help=ARGHELP_NETWORK)
parser.add_argument(
'-a', '--addresses', type=str, metavar='NETS',
help=ARGHELP_ADDRESSES)
parser.add_argument(
'-s', '--separator', type=str, metavar='SEP',
help=ARGHELP_SEPARATOR, default='\n')
parser.add_argument(
'-p', '--prefix', type=str, metavar='STR',
help=ARGHELP_PREFIX, default='')
parser.add_argument(
'-P', '--postfix', type=str, metavar='STR',
help=ARGHELP_POSTFIX, default='')
parser.add_argument(
'-i', '--ignore', action='store_true', help=ARGHELP_IGNORE)
return parser.parse_args()
def _die(code: int, message: Optional[str] = None) -> NoReturn:
if message:
if code != 0:
out = sys.stderr
else:
out = sys.stdout
print(message, file=out)
sys.exit(code)
def _is_string_a_valid_ip_address(item: str) -> bool:
try:
ip_address(item)
return True
except ValueError:
return False
def _is_string_a_valid_ip_network(item: str, strict: bool = False) -> bool:
if not strict:
try:
ip_network(item)
return True
except ValueError:
return False
else:
return bool(_is_string_a_valid_ip_network(item) and not \
_is_string_a_valid_ip_address(item))
def exclude_addresses(
target_network: Union[IPv4Network, IPv6Network],
addresses_to_exclude: Union[List[IPv4Network], List[IPv6Network]]
) -> Union[Iterator[IPv4Network], Iterator[IPv6Network]]:
addresses_to_exclude = sorted(collapse_addresses(addresses_to_exclude))
# Process addresses.
networks: Union[List[IPv4Network], List[IPv6Network]] = []
for address in addresses_to_exclude:
if address.subnet_of(target_network):
networks.extend(target_network.address_exclude(address))
networks = sorted(set(networks))
# Post-process resulting network list.
networks_to_remove = []
for network in networks:
for address in addresses_to_exclude:
if address.subnet_of(network) or address.supernet_of(network):
networks_to_remove.append(network)
for network in networks_to_remove:
if network in networks:
networks.remove(network)
return collapse_addresses(networks)
def validate_args(
target_net: str, addrs_str: str
) -> Union[tuple[Union[IPv4Network, IPv6Network], str], NoReturn]:
if not _is_string_a_valid_ip_network(target_net):
_die(1, f"{target_net} is not a valid ip network.")
elif not addrs_str:
_die(2, f"Missing addresses argument. It must be a {ARGHELP_ADDRESSES}.")
target_net = ip_network(target_net)
addrs_str = str(addrs_str).strip()
return target_net, addrs_str
def process_args(
target_net: Union[str, IPv4Network, IPv6Network],
addrs_str: str
) -> Union[tuple[set, set, set, set], NoReturn]:
addr_objs = set()
inv_addrs = set()
mis_addrs = set()
irr_addrs = set()
if _is_string_a_valid_ip_network(addrs_str):
net_a = ip_network(addrs_str)
if not isinstance(net_a, type(target_net)):
mis_addrs.add(net_a)
elif not net_a.subnet_of(target_net):
if net_a.supernet_of(target_net):
irr_addrs.add(net_a)
else:
irr_addrs.add(net_a)
else:
addr_objs.add(net_a)
else:
if ',' in addrs_str:
addr_list = addrs_str.split(',')
addrs = set(a.strip() for a in addr_list if a.strip() != '')
else:
if ' ' not in addrs_str:
_die(2, f"{addrs_str} is not a valid ip network.")
addr_list = addrs_str.split()
addrs = set(a.strip() for a in addr_list if a.strip() != '')
for a in addrs:
if not _is_string_a_valid_ip_network(a, strict=False):
inv_addrs.add(a)
continue
net_a = ip_network(a)
if not isinstance(net_a, type(target_net)):
mis_addrs.add(net_a)
elif not net_a.subnet_of(target_net):
irr_addrs.add(a)
else:
addr_objs.add(net_a)
return addr_objs, inv_addrs, mis_addrs, irr_addrs
def print_errors_and_exit(inv_addrs, mis_addrs, irr_addrs) -> NoReturn:
wrong_stuff_message_list = []
for wrong_stuff in zip(
('invalid address', 'misfitting address', 'irrelevant address'),
(inv_addrs, mis_addrs, irr_addrs)):
wrong_stuff_len = len(wrong_stuff[1])
if wrong_stuff_len > 0:
plural = 'es' if wrong_stuff_len > 1 else ''
wrong_stuff_str = ' '.join(str(item) for item in wrong_stuff[1])
wrong_stuff_message_list.append(
f"{wrong_stuff[0] + plural + ': ' + wrong_stuff_str}"
)
_die(2, '\n'.join(wrong_stuff_message_list).strip())
def print_result_and_exit(result_nets, separator, prefix, postfix) -> NoReturn:
_die(0, separator.join(
(prefix+str(n)+postfix for n in result_nets)
).strip())
def main() -> NoReturn:
args = parse_arguments()
separator = args.separator
prefix = args.prefix
postfix = args.postfix
target_net, addrs_str = validate_args(args.network, args.addresses)
addr_objs, inv_addrs, mis_addrs, irr_addrs = process_args(target_net,
addrs_str)
if not args.ignore and (inv_addrs or mis_addrs or irr_addrs):
print_errors_and_exit(inv_addrs, mis_addrs, irr_addrs)
else:
result_nets = sorted(list(
exclude_addresses(target_net, (a for a in addr_objs))
))
if len(result_nets) == 0:
_die(0, target_net)
print_result_and_exit(result_nets, separator, prefix, postfix)
if __name__ == '__main__':
main()