Last active
April 22, 2018 11:29
-
-
Save darkk/926c3ef9673d5ed794f2ce1632ab996c to your computer and use it in GitHub Desktop.
Токсичный реестр... лишние префиксы... бр-р-р!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import ipaddress | |
import sys | |
nets = [ipaddress.ip_network(line.strip()) | |
for line in open('subnets.txt')] | |
nets.sort() | |
def do_pass(): | |
for i in range(len(nets)): | |
for j in range(i+1, len(nets)): | |
if nets[i].overlaps(nets[j]): | |
todel = max((i, j), key=lambda x: nets[x].prefixlen) | |
print(nets[i], '&', nets[j], '=> del', nets[todel], file=sys.stderr) | |
del nets[todel] | |
return True | |
return False | |
while do_pass(): | |
pass | |
addr_count = 0 | |
for _ in nets: | |
print(_) | |
addr_count += _.num_addresses | |
for line in open('ips.txt'): | |
addr = ipaddress.ip_address(line.strip()) | |
for n in nets: | |
if addr in n: | |
print(addr, '&', n, '=> del', addr, file=sys.stderr) | |
break | |
else: | |
print(addr) | |
addr_count += 1 | |
print('Total:', addr_count, 'IP blacklisted', file=sys.stderr) |
Author
darkk
commented
Apr 21, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment