Question

How can I convert a netmask to a specific netmask.

Example: 127.0.0.0/16 Convert to /24 netmask.

It should output something like:

127.0.0.0/24
127.0.1.0/24
127.0.2.0/24
127.0.2.0/24

etc. all the way up to 127.0.255.0/24.

How can this be accomplished through PHP? I found this tool: http://jodies.de/ipcalc it's coded in PERL. How can I get similar funcitonality using PHP?

I found this: http://jasonkolpin.com/classes/ipcalc/demo.php although it seems broken.. Probably will try and fix it.

Update SOLVED: I basically run: $exec = shell_exec("/usr/local/bin/ipcalc 127.0.0.0/16 24 -b -n"); and parse out the Network: field. This basically converts 127.0.0.0/16 to all /24s.

Make sure that ipcalc is installed on the host to accomplish this. You can do this by using yum install ipcalc in CentOS, or apt-get install ipcalc in Ubuntu, or cd /usr/ports/net-mgmt/ipcalc/ && make install clean on FreeBSD :)

Was it helpful?

Solution

I've build a library to do IP address manipulation in pure PHP.

You'd have to do this:

<?php
use Leth\IPAddress\IP;
$current_cidr = 16;
$desired_cidr = 24;
$net = IP\NetworkAddress::factory('127.0.0.0', $current_cidr);
$subnets = $net->split($desired_cidr - $current_cidr);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top