Pejman Moghadam / Scripts

PHP Script - Subnet Info

This is a php function for getting subnet information.

Public domain


Syntax

    subnet_info($network, $request);

    $network can be "ip/netmask" or "ip/prefix"
    $request can be host number or one of this strings:
            ip, netmask, wildcard, prefix, netid, broadcast,
            ip_bin, netmask_bin, netid_bin, broadcast_bin,
            hosts

Usage example

$network = "192.168.50.70/255.255.255.240";
echo subnet_info($network,"ip")." : IP\n";
echo subnet_info($network,"netmask")." : Netmask\n";
echo subnet_info($network,"wildcard")." : Wildcard\n";
echo subnet_info($network,"networkid")." : Network ID\n";
echo subnet_info($network,"broadcast")." : Broadcast\n";
echo subnet_info($network,"ip_bin")." : IP binary\n";
echo subnet_info($network,"netmask_bin")." : Netmask binary\n";
echo subnet_info($network,"networkid_bin")." : NetID binary\n";
echo subnet_info($network,"broadcast_bin")." : Bradcast binary\n";
echo subnet_info($network,"prefix_bin")." : Prefix binary\n";
echo subnet_info($network,"prefix")." : Prefix\n";
echo subnet_info($network,"hosts")." : Hosts\n";
echo subnet_info($network,"12")." : Host number 12 \n";
echo "\n";

$network = "192.168.50.70/20";
echo subnet_info($network,"ip")." : IP\n";
echo subnet_info($network,"netmask")." : Netmask\n";
echo subnet_info($network,"wildcard")." : Wildcard\n";
echo subnet_info($network,"networkid")." : Network ID\n";
echo subnet_info($network,"broadcast")." : Broadcast\n";
echo subnet_info($network,"ip_bin")." : IP binary\n";
echo subnet_info($network,"netmask_bin")." : Netmask binary\n";
echo subnet_info($network,"networkid_bin")." : NetID binary\n";
echo subnet_info($network,"broadcast_bin")." : Bradcast binary\n";
echo subnet_info($network,"prefix_bin")." : Prefix binary\n";
echo subnet_info($network,"prefix")." : Prefix\n";
echo subnet_info($network,"hosts")." : Hosts\n";
echo subnet_info($network,"1200")." : Host number 1200 \n";
echo "\n";

Output example

192.168.50.70 : IP
255.255.255.240 : Netmask
0.0.0.15 : Wildcard
192.168.50.64 : Network ID
192.168.50.79 : Broadcast
11000000.10101000.00110010.01000110 : IP binary
11111111.11111111.11111111.11110000 : Netmask binary
11000000.10101000.00110010.01000000 : NetID binary
11000000.10101000.00110010.01001111 : Bradcast binary
11111111.11111111.11111111.11110000 : Prefix binary
28 : Prefix
16 : Hosts
192.168.50.75 : Host number 12

192.168.50.70 : IP
255.255.240.0 : Netmask
0.0.15.255 : Wildcard
192.168.48.0 : Network ID
192.168.63.255 : Broadcast
11000000.10101000.00110010.01000110 : IP binary
11111111111111111111000000000000 : Netmask binary
11000000.10101000.00110000.00000000 : NetID binary
11000000.10101000.00111111.11111111 : Bradcast binary
11111111111111111111000000000000 : Prefix binary
20 : Prefix
4096 : Hosts
192.168.52.175 : Host number 1200

subnet_info function

<?php

function subnet_info($network,$request)
{
    $slash_pos = strpos($network, '/');
    $netmask = substr($network, $slash_pos + 1);
    $ip = substr($network, 0, $slash_pos);

    // Change prefix to netmask
    if (strpos($netmask, '.') === false) {
            $prefix =  intval($netmask);
            $prefix_binary = str_pad(str_pad("",  $prefix, "1", STR_PAD_LEFT), 32, "0");
            $netmask = bindec (substr ($prefix_binary, 0 ,8));
            $netmask = $netmask.".".bindec (substr ($prefix_binary, 8 ,8));
            $netmask = $netmask.".".bindec (substr ($prefix_binary, 16 ,8));
            $netmask = $netmask.".".bindec (substr ($prefix_binary, 24 ,8));
    }

    $ip_array = explode('.', $ip);
    $netmask_array = explode('.', $netmask);

    $hosts = 1;
    for($i=0; $i<4; $i++)
    {
            $ip_quarter = $ip_array[$i];
            $netmask_quarter = $netmask_array[$i];

            $ip_binary_array[$i] = str_pad(decbin($ip_quarter), 8, "0", STR_PAD_LEFT);
            $netmask_binary_array[$i] = str_pad(decbin($netmask_quarter), 8, "0", STR_PAD_LEFT);

            $netmask_quarter = intval($netmask_quarter);
            $ip_quarter = intval($ip_quarter);

            $count = 256 - $netmask_quarter;
            $begin = intval($ip_quarter / $count) * $count;
            $end = $begin + $count - 1; 
            $hosts *= $count;

            $begin_array[$i] = $begin;
            $end_array[$i] = $end;

            $begin_binary_array[$i] = str_pad(decbin($begin), 8, "0", STR_PAD_LEFT);
            $end_binary_array[$i] = str_pad(decbin($end), 8, "0", STR_PAD_LEFT);

            $wildcard_array[$i] = $netmask_quarter ^ 255;
    }

    $networkid = implode('.', $begin_array);
    $broadcast = implode('.', $end_array);
    $wildcard = implode('.', $wildcard_array);

    $networkid_binary = implode('.', $begin_binary_array);
    $broadcast_binary = implode('.', $end_binary_array);

    $ip_binary = implode('.', $ip_binary_array);
    $netmask_binary = implode('.', $netmask_binary_array);

    if (!isset($prefix)) {
            $prefix_binary = $netmask_binary;
            $prefix = 0;
            for($i = 0; $i < 32; $i++)
                    if($prefix_binary[$i] == "1")
                            $prefix++;
    }

    switch (strtolower($request)) {
            case "ip" :
                    return $ip;
                    break;
            case "netmask" :
            case "mask" :
                    return $netmask;
                    break;
            case "prefix" :
                    return $prefix;
                    break;
            case "netid" :
            case "network" :
            case "networkid" :
                    return $networkid;
                    break;
            case "broadcast" :
                    return $broadcast;
                    break;
            case "ipbin" :
            case "ip_bin" :
                    return $ip_binary;
                    break;
            case "netmaskbin" :
            case "netmask_bin" :
            case "maskbin" :
            case "mask_bin" :
            case "prefix_bin" :
            case "prefixbin" :
                    return $prefix_binary;
                    break;
                case "netid_bin" :
                case "network_bin" :
                case "networkid_bin" :
                        return $networkid_binary;
                        break;
            case "broadcast_bin" :
            case "broadcastbin" :
                    return $broadcast_binary;
                    break;
            case "wildcard" :
                    return $wildcard;
                    break;
            case "hosts" :
            case "count" :
                    return $hosts;
                    break;
            default :
                    $request = intval($request);
                    $num = 0;
                    for ($j[0] = $begin_array[0]; $j[0] <= $end_array[0]; $j[0]++)
                    for ($j[1] = $begin_array[1]; $j[1] <= $end_array[1]; $j[1]++)
                    for ($j[2] = $begin_array[2]; $j[2] <= $end_array[2]; $j[2]++)
                    for ($j[3] = $begin_array[3]; $j[3] <= $end_array[3]; $j[3]++) {
                        $host_ip = "$j[0].$j[1].$j[2].$j[3]";
                        $num++;
                        if ($request == $num) return $host_ip;
                    }
    }
}

?>

BY: Pejman Moghadam
TAG: php
DATE: 2012-07-02 18:14:16


Pejman Moghadam / Scripts [ TXT ]