Prefix Merge

Yesterday I showed how to get the list of prefixes announced by an ASN and I asked whether theses prefixes could be merged.

A similar question was posted on perlmonks.org: Challenge Problem: Merging Network Address back in 2001. Does anyone remember Perl?

I have a long list of bad hosts, containing addresses and subnets, can it be reduced?

Using this short program to merge the objects, my list went from 8228 to 6800 lines.

#! /usr/bin/env python

import netaddr as na

ip_list = []

with open("badhosts.lst") as f:
    lines = f.readlines()

for line in lines:
    line = line.strip()
    if "/" in line:
        ip_list.append(na.IPNetwork(line))
    else:
        ip_list.append(na.IPAddress(line))

for e in na.cidr_merge(ip_list):
    if e.version == 4 and e.prefixlen == 32:
        print(e[0])
    else:
        print(e)

Reference: netaddr, a Python library for manipulating addresses and networks.

2024-12-22


copyright 2025 mobilarte