K
K
Kirill2020-10-27 11:28:41
PHP
Kirill, 2020-10-27 11:28:41

Does anyone know a good library for converting an IP range to CIDR(s)?

I can't find a normal PHP library to convert a range of IP addresses to CIDRs (or CIDRs). That is, give it an input, for example, 10.0.23.40-10.0.25.241and get something like the output

10.0.23.40/29
10.0.23.48/28
10.0.23.64/26
10.0.23.128/25
10.0.24.0/24
10.0.25.0/25
10.0.25.128/26
10.0.25.192/27
10.0.25.224/28
10.0.25.240/31

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nokimaro, 2020-10-27
@init0ru

https://github.com/S1lentium/IPTools
Able to solve a wide range of tasks that arise when working with addresses, subnets, and so on.

$range_str = '10.0.23.40-10.0.25.241';
$networks = IPTools\Range::parse($range_str)->getNetworks();
foreach($networks as $network)
{
     echo (string)$network."\n";
}

A
Armenian Radio, 2020-10-27
@gbg

Write yourself, this is a simple task
- see how many zero bits are at the end of the address -> glue ADDRESS / HOW MANY BITS -
turn all these bits into ones. Add 1.
-repeat until you pass the entire range.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question