Untitled

mail@pastecode.io avatar
unknown
python
10 months ago
1.3 kB
5
Indexable
Never
input_file = "base.txt"
badIPs_file = "badIPs.txt"
goodIPs_file = "goodIPs.txt"

MAX_PORTS_FOR_GOOD = 15

# Используем defaultdict для автоматического создания списка портов для каждого IP
ip_ports = {}
black_ips_mask = {}
# Открываем файл и читаем строки
with open(input_file, 'r', encoding='utf-8') as file:
    for line in file:
        ip_port = line.strip()
        ip, port = ip_port.split(':')
        if ip in ip_ports: ip_ports[ip]=1
        else: ip_ports[ip]+=1

with open(badIPs_file, 'w', encoding='utf-8') as badIPs_file_w:
    for ip in ip_ports:
        if ip_ports[ip] > MAX_PORTS_FOR_GOOD:
            black_ip_mask, _ = ip.rsplit('.',1)
            black_ips_mask[black_ip_mask]=None
            badIPs_file_w.write(f"{black_ips_mask}.1/24\n")
del ip_ports

with open(input_file, 'r', encoding='utf-8') as file:
    with open(goodIPs_file, 'w', encoding='utf-8') as goodIPs_file_w:
        for line in file:
            ip_port = line.strip()
            ip, port = ip_port.split(':')
            black_ip_mask, _ = ip.rsplit('.',1)
            if black_ip_mask in black_ips_mask:
                continue
            goodIPs_file_w.write(f"{ip_port}\n")

Leave a Comment