andrewandante / submuncher
Tool for consolidating subnets
Requires
- php: >=5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: ^3.2
README
Handy tool for consolidating subnets into as few subnets as possible
Installation
composer require andrewandante/submuncher
Using it
The most basic usage is to pass an array of IPv4 addresses into AndrewAndante\SubMuncher\SubMuncher::consolidate()
The most useful usage is to pass an array of CIDRs into AndrewAndante\SubMuncher\SubMuncher::consolidate_subnets()
There are also a bunch of helper IP utility functions in the Util
classe should you need to do some tweaking.
You can also pass a second parameter that limits the number of rules returned. This invokes some magic to combine some subnets in a way that introduces the least number of additional IP addresses into the range as possible.
For example, to reduce your list of subnets down to 25 total rules, try AndrewAndante\SubMuncher\SubMuncher::consolidate_subnets($cidrsArray, 25);
Debugging
There are verbose methods which will give you the initial list of IPs covered by your CIDRs, and the ultimate list covered. That will give you the opportunity to compare, so you can see what additional IPs have been introduced. This is in case you are using them for a whitelist or something that requires exact knowledge of the IPs.
Warning
This is significantly slower when calculating subnets, as it expands out all the individual IPs in the subnet, rather than just using the first and last IP
Limitations
Only tested on IPv4 at present.