alistairphillips.com

I’m : a web and mobile developer based in the Australia.


IP CIDR calculations

After a bit of Googling 12 I manage to put together a PHP script to convert IP CIDR notation into IP ranges.

<?php
    $mask = '30';
    $firstIp = ip2long('127.0.0.1');
    $lastIp = $firstIp + pow(2, (32-$mask)) - 1;

    echo 'Network Address   : ' . long2ip( $firstIp ) . PHP_EOL;
    echo 'Broadcast Address : ' . long2ip( $lastIp ) . PHP_EOL;
    echo PHP_EOL;

    for ( $iip = $firstIp; $iip <= $lastIp; $iip++ ) {
        echo long2ip( $iip ) . PHP_EOL;
    }
?>

With it outputting this:

Network Address   : 127.0.0.1
Broadcast Address : 127.0.0.4
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4