Last active
February 19, 2018 23:05
-
-
Save christiannageby/1510c895ec68c4fd7bf795e1acafc292 to your computer and use it in GitHub Desktop.
Tool that checks if a given IP-address is valid in a specified subnet. The tool also specefies if the checked IP-address is a Network ID or Broadcast address and echo out the result. The tool is a php script that can be ran in CLI by any client with php installed. Use the tool this way: php ip_subnet_validator.php [IP] [SUBNET] Example: php ip_s…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(0); | |
print("\n"); | |
$slashpos = strpos($argv[2], '/'); //get the position of the '/' in $argv[1] | |
$checkIP = $argv[1]; | |
$ip = substr($argv[2], 0, $slashpos); //Use this to break out the ip and cidr-number | |
//if the ip is shorter than 4 chars (x.x.x.x) or if the ip is logner than 16 chars xxx.xxx.xxx.xxx | |
//kill the script and tell user that ip is invalid | |
if(strlen($ip) > 15 || strlen($ip) < 7){ | |
die("Invalid ip"); | |
} | |
//check if a fraction of a ip fr.fr.fr.fr has a value of over 255 assume that the ip is invalid | |
//also convert the ip address to binary Array | |
$ipFractions = explode(".", $ip); | |
$ipBinary = array(); | |
foreach ($ipFractions as $fraction) { | |
if($fraction > 255 || $fraction < 0){ | |
die("Invalid ip"); | |
} | |
array_push($ipBinary, sprintf( "%08d", decbin($fraction))); | |
} | |
//if the cidr number is greater than 32 kill the script and tell the user that the CIDR number is invalid. | |
$cidr = substr($argv[2], $slashpos+1); | |
if($cidr > 32 ||$cidr < 0){ | |
die("Invalid CIDR input"); | |
} | |
//make a new array and push 1 or zeros to it (convert cidr to binary subnetmask) | |
$subnetBinary = array(); | |
for($i = 1; $i <= 32; $i++){ | |
if($i <= $cidr){ | |
array_push($subnetBinary, '1'); | |
}else{ | |
array_push($subnetBinary, '0'); | |
} | |
} | |
$subnetBinary = implode($subnetBinary); //convert the char array to a string variable. | |
//make an array of the subnet in .-separated fractions | |
$subnetBinaryArray = array(); | |
array_push($subnetBinaryArray, substr($subnetBinary, 0, 8) ); | |
array_push($subnetBinaryArray, substr($subnetBinary, 8, 8) ); | |
array_push($subnetBinaryArray, substr($subnetBinary, 16, 8) ); | |
array_push($subnetBinaryArray, substr($subnetBinary, 24, 8) ); | |
//get the first ip in binary. | |
$first = array(); | |
for ($i = 0; $i < 4; $i++){ | |
$fraction = array(); | |
for($j = 0; $j < 8; $j++){ | |
array_push($fraction, ( substr($subnetBinaryArray[$i], $j, 1) * substr($ipBinary[$i], $j, 1) ) ); | |
} | |
array_push($first, implode("", $fraction)); | |
} | |
//get the last ip in binary | |
$lastStr = array(); | |
for($k = 0; $k < 32; $k++){ | |
$netmask = substr($subnetBinary, $k, 1); | |
$ipaddr = substr(implode($ipBinary), $k, 1); | |
if($netmask == 0){ | |
$num = 1; | |
}else{ | |
$num = $ipaddr; | |
} | |
array_push($lastStr, $num); | |
} | |
//convert the last ip to an array splited by . e.g array(192, 168, 0, 1) | |
$last = array(); | |
array_push($last, substr( implode($lastStr), 0, 8) ); | |
array_push($last, substr( implode($lastStr), 8, 8) ); | |
array_push($last, substr( implode($lastStr), 16, 8) ); | |
array_push($last, substr( implode($lastStr), 24, 8) ); | |
//convert the last and first ip in range to decimal | |
$min = array(); | |
$max = array(); | |
foreach($last as $b){ | |
array_push($max, bindec($b)); | |
} | |
foreach($first as $b){ | |
array_push($min, bindec($b)); | |
} | |
//check if ip is in range | |
if( (ip2long(implode(".", $min)) <= ip2long($checkIP)) && ( ip2long($checkIP) <= ip2long(implode(".", $max)))){ | |
print("True"); | |
if (ip2long(implode(".", $min)) == ip2long($checkIP)){ | |
print("Net id"); | |
}elseif (ip2long(implode(".", $max)) == ip2long($checkIP)){ | |
print("broadcast"); | |
} | |
}else{ | |
print("False"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment