开发者

Enumerating IPv4 Addresses from Shorthand in PHP

I need some help with a PHP function I am writing. I need to take input like this:

192.168.1.1-255
or
192.168.1.1/28

and turn it into an array of addresses, such as:

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
...

Here's where I am at (LOL, not far at all):

$remoteAddresses = array('192.168.1.1-255);开发者_JS百科

foreach($remoteAddresses as &$address) {
  if(preg_match('/(.*)(-\n*)/', $address, $matches)) {
    $address = $matches[1];
  }
}

If anyone has some spare time and wants to help me, I really don't know how I am going to handle the 192.168.1.1/28 syntax...


I would use the ip2long() and long2ip() to perform the IP address calculations. Proving the / syntax means CIDR, it would be something like:

    $remoteAddresses = array('192.168.1.1-5', 
                             '73.35.143.32/27', 
                             '73.35.143.32/30', 
                             '73.35.143.32/32',  
                             '192.168.1.18/25');


    foreach($remoteAddresses as $address) {
      echo "\nRange of IP addresses for $address:\n";
      if(preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(\-|\/)([0-9]+)$/', $address, $matches)) {
        $ip = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.' . $matches[4];
        $ipLong = ip2long($ip);
        if ( $ipLong !== false ) {

            switch( $matches[5] ) {
                case '-':
                    $numIp = $matches[6];
                    break;
                case '/':
                    $cidr = $matches[6];
                    if ( $cidr >= 1 && $cidr <= 32 ) {
                        $numIp = pow(2, 32 - $cidr); // Number of IP addresses in range
                        $netmask = (~ ($numIp - 1)); // Network mask
                        $ipLong = $ipLong & $netmask; // First IP address (even if given IP was not the first in the CIDR range)
                    }
                    else {
                        echo "\t" . "Specified CIDR " . $cidr . " is invalid (should be between 1 and 32)\n";
                        $numIp = -1;
                    }
                    break;
            }

            for ( $ipRange = 0 ; $ipRange < $numIp ; $ipRange++) {
                echo "\t" . long2ip($ipLong + $ipRange) . "\n";
            }      
        }
        else {
            echo "\t" . $ip . " is invalid\n";
        }   
      }
      else  {
          echo "\tUnrecognized pattern: " . $address . "\n";
      }

    }


You can try the following. Instead of printing, you can add the result to the array you want to build.

$remoteAddresses = array('192.168.1.1-5', '192.168.1.18/25');

foreach($remoteAddresses as $address) {
  if(preg_match('/([0-9\.]+)\.([0-9]+)(\/|\-)([0-9]+)$/', $address, $matches)) {
    $range = range($matches[2], $matches[4]);
    foreach ($range as $line) {
      echo $matches[1] . '.' . $line . '<br />';
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜