2 Commits
v0.1 ... v0.2

2 changed files with 120 additions and 15 deletions

24
README.md Normal file
View File

@@ -0,0 +1,24 @@
# Netcalc #
This is a simple CLI network calculator made in PHP. Usage is pretty straight
forward. For example, let say you want to know how many usable addresses you
will get with a 26 bit netmask, simply run the script as below:
php netcalc.php 26
The above command will give you the following outut:
Netmask
-------
In slash notation: 26
In dotted decimal: 255.255.255.192
In dotted binary: 11111111.11111111.11111111.11000000
Network size
------------
Total number of addresses: 64
Number of usable addresses for hosts: 62
## Copyright ##
Netcalc is written by Jack-Benny Persson and released under GNU GPL version 2.

View File

@@ -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,30 +32,56 @@ function print_usage()
print "Example: php netcalc.php 24\n"; print "Example: php netcalc.php 24\n";
} }
// Check if first argument is set and and if not, print usage function print_netmask($number)
if (!isset($argv[1]))
{ {
print_usage(); $number = $number-1;
exit(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 binary format
$dotted_binary = substr_replace($binary, ".", 8, 0);
$dotted_binary = substr_replace($dotted_binary, ".", 17, 0);
$dotted_binary = substr_replace($dotted_binary, ".", 26, 0);
// 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 dotted decimal: $firstOctet.$secondOctet.$thirdOctet.$forthOctet\n";
print "In dotted binary: $dotted_binary";
print "\n\n";
} }
$value=$argv[1]; function print_values($value)
// Check if our argument was a numeric value or not
if (!is_numeric($value))
{ {
print "Please enter numbers only (between 0 and 32)\n"; // Calculate the network size and usable addresses
exit(1);
}
// Check if the value is between 0 and 32
if ($value < 0 || $value > 32)
{
print "Please enter an integer between 0 and 32 only\n";
exit(1);
}
// Calculate the diffrent values
$base=32-$value; $base=32-$value;
$netsize=pow(2, $base); $netsize=pow(2, $base);
$hosts=$netsize-2; $hosts=$netsize-2;
@@ -42,10 +90,43 @@ if ($hosts < 0)
$hosts = 0; $hosts = 0;
} }
// And now, finally, print the values // Print the network size and usable addresses
print "Netmask: $value\n"; print "Network size\n";
print "------------\n";
print "Total number of addresses: $netsize\n"; print "Total number of addresses: $netsize\n";
print "Number of usable addresses for hosts: $hosts\n"; print "Number of usable addresses for hosts: $hosts\n";
}
// MAIN
// Check if first argument is set and and if not, print usage
if (!isset($argv[1]))
{
print_usage();
exit(1);
}
$input=$argv[1];
// Check if our argument was a numeric value or not
if (!is_numeric($input))
{
print "Please enter numbers only (between 0 and 32)\n";
exit(1);
}
// Check if the value is between 0 and 32
if ($input < 0 || $input > 32)
{
print "Please enter an integer between 0 and 32 only\n";
exit(1);
}
// Print the netmask first
print_netmask($input);
// And now, finally, print the network size and usable addresses
print_values($input);
exit(0); exit(0);
?> ?>