master
FiliN 2 years ago
parent e20a70841e
commit 2c57d41527
  1. 28
      subnets.py

@ -1,17 +1,23 @@
#!/usr/bin/python
import ipaddress
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--test', action='store_const', const=True, help='Тестовый режим')
args = parser.parse_args()
def read_ips():
prefixes = {}
for line in sys.stdin:
prefix = ipaddress.ip_network(line.strip('\n')).network_address.compressed
prefixlen = ipaddress.ip_network(line.strip('\n')).prefixlen
if prefixlen < 32:
for ip in ipaddress.ip_network(prefix + '/' + str(prefixlen)):
prefixes[ip.compressed] = 32
prefixes[prefix] = prefixlen
line = line.strip('\n')
try:
if ipaddress.ip_network(line) or ipaddress.ip_address(line):
prefix = ipaddress.ip_network(line).network_address.compressed
prefixlen = ipaddress.ip_network(line).prefixlen
prefixes[prefix] = prefixlen
except ValueError:
continue #bad subnet address
return prefixes
@ -36,8 +42,10 @@ def subnets_join(prefixes, new_prefixlen):
prefixes = read_ips()
maxprefixlen = 32
print('/{} - {}'.format(maxprefixlen, len(prefixes)))
for prefixlen in range(maxprefixlen - 1, 0, -1):
maxprefixlen,start_cnt = 32, len(prefixes)
for prefixlen in range(maxprefixlen, 0, -1):
prefixes = subnets_join(prefixes, prefixlen)
print('/{} - {}'.format(prefixlen, len(prefixes)))
if args.test:
print('/{} - {} - {}%'.format(prefixlen, len(prefixes), int(100 / start_cnt * len(prefixes))))
if not args.test:
[print('{}/{}'.format(pref, preflen)) for pref, preflen in prefixes.items()]

Loading…
Cancel
Save