Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代IP';PHP中给定参数的_Php_Ip_Iteration - Fatal编程技术网

迭代IP';PHP中给定参数的

迭代IP';PHP中给定参数的,php,ip,iteration,Php,Ip,Iteration,我有以下代码: $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)

我有以下代码:

$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++;
}
}
我想用它来检查iprange是否存在某个目录。 我已经完成了curl代码,但是我缺少的是我的IP生成算法

我想用以下方式调用脚本: php script.php 1.2.3

然后脚本将遍历1.2.3.1->1.2.3.255 然而,这是可行的: php脚本。php 1.2。仅进行1.2.1->1.2.255的交互 这不是真正的iprange,这有点破坏了我的程序


有人能帮忙吗?

我冒昧地修改了一下您的代码:

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]);
返回在ip中找到
\d{1,3}\.
的次数,即当前深度

if($depth < 3)

这会再次调用相同的函数,但ip长一个号码

用法:
php ips.php 253.168.11.2 253.168.11.3
。适用于任何范围,包括1.1.1.1至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;
}

这个答案让我更加困惑,本雅明认为最好的答案是正确的(在投票计数器旁边的V符号)。
scan_ips($ip."$i.");
<?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;
}