Subnet Merge

Up ../

Summarize, condense or merge network/mask lists

A while ago I was looking for something to summarize network access lists where the lists were either /1..32 or /dotted quad format. I found a few perl and some python scripts but they produced conflicting output for the same files and even when the same file had its line ordering changed.

I thought about the easiest way of doing this and one way of sorting the entries and coalescing adjacent entries seemed reasonable (cidr_merge does this.) The other was to build a binary tree and merge adjacent entries while inserting into the tree. I found the second easier to implement.


subnet_merge [ -D ] [ -m C|N|W ] -f network_mask_list_file
               -D	debug info
               -m	mask style for list output
                   C	xx.xx.xx.0/cc		[C]idr
                   N	xx.xx.xx.0/	[N]etmask
                   W	xx.xx.xx.xx		tcp_[W]rapper
               -f file	input file one entry per line


Given an input file containing this list
the program should write to stdout
The input could equivalently be given as
To have the output printed as
use the -m N switch. The -m W switch is identical except for masks for host addresses.
eg instead of

If the list is too big for the program to handle in one go sort the list and split the output into a number of pieces Run subnet_merge on the pieces separately and then on the output.


Sources at Sources in files/

The code doesn't use any networking headers or libraries and should be compilable by any C89 compiler. Unsigned int has to be 4 bytes or more (otherwise change unsigned int to unsigned long.)



Artistic or
GPL or
Creative Commons CC0 [prefer]


James Sainsbury