Benjamin Shehu
Benjamin Shehu

Reputation: 3

Iterate IP's in PHP from given args

I have the following code:

$ipgiven = $argv[1];
//REGEX PATTERNS
$patternone = "/^\d{1,3}\.$/";
$patterntwo = "/\d{1,3}\.\d{1,3}\./";
$patternthree = "/\d{1,3}\.\d{1,3}\.\d{1,3}\./";


if(preg_match($patternthree, $ipgiven)){
echo "found depth of 3\r\n";
while($i!=255){
$ipo = $ipgiven . $i;

while($j!=255){
$ipd = $ipo . $j;

while($k!=255){
$ipt = $ipd . $k;
$checkme = $prefix . $ipt . $find;
checkurl($checkme);
$k++;
}

$j++;
}

$i++;
}
}

if(preg_match($patterntwo, $ipgiven)){
echo "found depth of 2\r\n";
while($i!=255){
$ipo = $ipgiven . $i;

while($j!=255){
$ipd = $ipo . $j;
$checkme = $prefix . $ipd . $find;
checkurl($checkme);
$j++;
}

$i++;
}
}

if(preg_match($patternone, $ipgiven)){
echo "found depth of 1\r\n";
while($i!=255){
$ipo = $ipgiven . $i;
$checkme = $prefix . $ipo . $find;
checkurl($checkme);
$i++;
}
}

I want to use it to check an iprange if a certain directory exists. I have the curl code worked out, however what I am lacking is my IP generation algorithm.

I thought of calling the script in the following way: php script.php 1.2.3.

and then the script would iterate through 1.2.3.1 -> 1.2.3.255 this works, however calling: php script.php 1.2. only makes an interation 1.2.1 -> 1.2.255 which isn't really an iprange, which is kind of breaking my program.

Can somebody please help?

Upvotes: 0

Views: 491

Answers (2)

oxygen
oxygen

Reputation: 6049

Usage: php ips.php 253.168.11.2 253.168.11.3. Works for any range, including such like 1.1.1.1 to 2.3.1.255.

<?php
if(!isset($argv[1]) || !isset($argv[2]))
{
    echo PHP_EOL."Invalid usage. Valid usage example: php ips.php 253.168.0.1 253.168.11.255".PHP_EOL;
    exit(1);
}
if(
    !filter_var($argv[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
    || !filter_var($argv[2], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
)
{
    echo PHP_EOL."Invalid IPv4 dotted format. Valid usage example: php ips.php 192.168.0.1 192.168.11.255".PHP_EOL;
    exit(1);
}

//IPv4 addresses are 32bit unsigned and the PHP integer is 32bit signed. 
//Because IPs greater than 0x7fffffff cannot be represented using int, string + BC Math functions are used instead of int + normal math operators.

//get string representation of unsigned integer
$strIPv4Start=sprintf("%u", ip2long($argv[1]));
$strIPv4End=sprintf("%u", ip2long($argv[2]));
$strIPv4Current=$strIPv4Start;

while(bccomp($strIPv4Current, $strIPv4End)<=0)
{
    $strIPv4Current=bcadd($strIPv4Current, "1");

    echo long2ip($strIPv4Current).PHP_EOL;
}

Upvotes: 0

primo
primo

Reputation: 1454

I took the liberty of modifying your code a bit:

function scan_ips($ip) {

  $depth = preg_match_all('/\d{1,3}\./', $ip, $m);

  for($i=1; $i<=255; $i++) {
    if($depth < 3) {
      scan_ips($ip."$i.");
    } else {
      checkurl($ip.$i);
    }
  }
}

scan_ips($argv[1]);

This should work as you expect it to.

EDIT

How it works:

$depth = preg_match_all('/\d{1,3}\./', $ip, $m)

Returns the number of times \d{1,3}\. was found in the ip, the current depth.

if($depth < 3)

If the depth as already three, we need only need to add one more number to generate a valid ip. If not we need to add more numbers. These numbers are added by making a recursive call:

scan_ips($ip."$i.");

This calls the same function again, but with an ip that's one number longer.

Upvotes: 1

Related Questions