Ok so i'm trying to count IP. I have a PHP version and a JQuery/Javascript version. PHP works a dream, except for not outputting to screen until the PHP script has finished but i'm ok with that. The issue I'm having is with the jquery/javascript version. Counting the 4th Octet is fine but when it comes to counting the 3rd Octet i'm having a strange issue.
Basically if my range is 10.211.55.254 - 10.211.56.2, i'm good. But if i was to increase to 10.211.56.3 or above ip addresses i get the following results:
10.211.55.250
10.211.56.250
This only happens if increase, reducing to below 10.211.55.254 still works.
So here are the scripts from each, where i'm i going wrong/missing on the Javascript?
The Javascript
var startiparrayslice = startiparray.slice( 0, 2);
for ( var octet3 = startiparray[2] ; octet3 <= endiparray[2]; octet3++) {
var pingip2 = startiparrayslice.join(".") + "." + octet3;
if (startiparray[3] > endiparray[3]) {
endiparrayorig = endiparray[3];
endiparray[3] = 255;
}
for ( var octet4 = startiparray[3] ; octet4 <= endiparray[3]; octet4++) {
if (octet4 == endiparray[3]) {
startiparray[3] = 0;
endiparray[3] = endiparrayorig;
}
var pingip = pingip2 + "." + octet4;
$.post("ping-query-process.php", {
pingformip:pingip
},
.
The PHP
$ipnetwork = array_slice($iprangestartex,0,2);
$ip = implode(".",$ipnetwork);
for ($octet3=$iprangestartex[2];$octet3<=$iprangeendex[2];$octet3++) {
$ipaddress = $ip . "." . $octet3;
if ($iprangestartex[3] > $iprangeendex[3]) {
$iprangeendexorig = $iprangeendex[3];
$iprangeendex[3] = "255";
}
for ($octet4=$iprangestartex[3];$octet4<=$iprangeendex[3];$octet4++) {
if ($octet4 == $iprangeendex[3]) {
$iprangestartex[3] = "0";
$iprangeendex[3] = $iprangeendexorig;
}
$ipaddress4 = $ipaddress . "." . $octet4;
$pingresult = exec("/bin/ping -c 1 $ipaddress4", $result, $status);
if (0 == $status) {
$status = "alive";
} else {
$status = "dead";
}
echo "The IP Address, $ipaddress4, is ".$status."<br /><br />";
}
}
Well I didn't see that one coming!! After testing testing testing and using plenty of alert boxes I narrowed it down to the fact that the array1[3] wasn't comparing to array2[3] for certain numbers. Don't ask me why only on certain numbers because I do not know (Answers on a postcard please).
I was able see that the array was trying to compare strings using array2[3].constructor when in actual fact what i needed to compare numbers. So to achieve this I included the following parseInt
if (parseInt(startiparray[3]) > parseInt(endiparray[3])) { }
This compared the array elements as numbers and I was able to iterate through 3-9 where before it would failover.
I would still like to understand why... As i'm new to development this is a sharp learning curve.
Thanks