Added a function to print the netmask in binary and dotted decimal form
This commit is contained in:
parent
ac2096776e
commit
4105721a08
107
netcalc.php
107
netcalc.php
@ -1,4 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (C) 2014 Jack-Benny Persson <jack-benny@cyberinfo.se>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Netcalc is a simple PHP CLI script to calculate the size of a network
|
||||||
|
from a given netmask in slash notation */
|
||||||
|
|
||||||
$version="0.1";
|
$version="0.1";
|
||||||
|
|
||||||
function print_usage()
|
function print_usage()
|
||||||
@ -10,6 +32,68 @@ function print_usage()
|
|||||||
print "Example: php netcalc.php 24\n";
|
print "Example: php netcalc.php 24\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function print_netmask($number)
|
||||||
|
{
|
||||||
|
$number = $number-1;
|
||||||
|
$mask = $number+1;
|
||||||
|
|
||||||
|
$ones = 1;
|
||||||
|
$zeros = 0;
|
||||||
|
|
||||||
|
$num_zeros = 32-$number-2;
|
||||||
|
|
||||||
|
// Itterate the binary ones, adding one 1 for every run
|
||||||
|
for ($i = 0; $i < $number; $i++)
|
||||||
|
{
|
||||||
|
$ones .= 1;
|
||||||
|
}
|
||||||
|
unset($i);
|
||||||
|
|
||||||
|
// Itterate the binary zeros, adding one 0 for every run
|
||||||
|
for ($i = 0; $i < $num_zeros; $i++)
|
||||||
|
{
|
||||||
|
$zeros .= 0;
|
||||||
|
}
|
||||||
|
unset($i);
|
||||||
|
|
||||||
|
// Put it together in a binary string
|
||||||
|
$binary = "$ones$zeros";
|
||||||
|
|
||||||
|
// Make it dotted decimal format
|
||||||
|
$firstOctet = base_convert(substr($binary, 0, 8), 2, 10);
|
||||||
|
$secondOctet = base_convert(substr($binary, 8, 8), 2, 10);
|
||||||
|
$thirdOctet = base_convert(substr($binary, 16, 8), 2, 10);
|
||||||
|
$forthOctet = base_convert(substr($binary, 24, 8),2, 10);
|
||||||
|
|
||||||
|
// Print it in different formats
|
||||||
|
print "Netmask\n" ;
|
||||||
|
print "-------\n";
|
||||||
|
print "In slash notation: $mask\n";
|
||||||
|
print "In binary: ";
|
||||||
|
print $binary . "\n";
|
||||||
|
print "In dotted decimal: $firstOctet.$secondOctet.$thirdOctet.$forthOctet";
|
||||||
|
print "\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function print_values($value)
|
||||||
|
{
|
||||||
|
// Calculate the network size and usable addresses
|
||||||
|
$base=32-$value;
|
||||||
|
$netsize=pow(2, $base);
|
||||||
|
$hosts=$netsize-2;
|
||||||
|
if ($hosts < 0)
|
||||||
|
{
|
||||||
|
$hosts = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the network size and usable addresses
|
||||||
|
print "Network size\n";
|
||||||
|
print "------------\n";
|
||||||
|
print "Total number of addresses: $netsize\n";
|
||||||
|
print "Number of usable addresses for hosts: $hosts\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// MAIN
|
||||||
// Check if first argument is set and and if not, print usage
|
// Check if first argument is set and and if not, print usage
|
||||||
if (!isset($argv[1]))
|
if (!isset($argv[1]))
|
||||||
{
|
{
|
||||||
@ -17,35 +101,28 @@ if (!isset($argv[1]))
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$value=$argv[1];
|
$input=$argv[1];
|
||||||
|
|
||||||
// Check if our argument was a numeric value or not
|
// Check if our argument was a numeric value or not
|
||||||
if (!is_numeric($value))
|
if (!is_numeric($input))
|
||||||
{
|
{
|
||||||
print "Please enter numbers only (between 0 and 32)\n";
|
print "Please enter numbers only (between 0 and 32)\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the value is between 0 and 32
|
// Check if the value is between 0 and 32
|
||||||
if ($value < 0 || $value > 32)
|
if ($input < 0 || $input > 32)
|
||||||
{
|
{
|
||||||
print "Please enter an integer between 0 and 32 only\n";
|
print "Please enter an integer between 0 and 32 only\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the diffrent values
|
// Print the netmask first
|
||||||
$base=32-$value;
|
print_netmask($input);
|
||||||
$netsize=pow(2, $base);
|
|
||||||
$hosts=$netsize-2;
|
// And now, finally, print the network size and usable addresses
|
||||||
if ($hosts < 0)
|
print_values($input);
|
||||||
{
|
|
||||||
$hosts = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// And now, finally, print the values
|
|
||||||
print "Netmask: $value\n";
|
|
||||||
print "Total number of addresses: $netsize\n";
|
|
||||||
print "Number of usable addresses for hosts: $hosts\n";
|
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user