Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting (E_ALL);
- //----------------------------------------------------------------------------------
- // Файл с диапазонами
- $fileIn = 'input.txt';
- // Результирующий файл с диапазонами в формате CIDR, подходящем для nmap
- $fileOut = 'output.txt';
- //----------------------------------------------------------------------------------
- /**
- * @author phptuts
- * @copyright 2012
- * @link http://tutorialspots.com/
- */
- function cidr2ip($cidr)
- {
- $ip_arr = explode('/', $cidr);
- $start = ip2long($ip_arr[0]);
- $nm = $ip_arr[1];
- $num = pow(2, 32 - $nm);
- $end = $start + $num - 1;
- return array($ip_arr[0], long2ip($end));
- }
- /**
- * @author phptuts
- * @copyright 2012
- * @link http://tutorialspots.com/
- */
- function valid_ip_cidr($cidr, $must_cidr = false)
- {
- if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\/[0-9]{1,2})?$/", $cidr))
- {
- $return = false;
- } else
- {
- $return = true;
- }
- if ($return == true)
- {
- $parts = explode("/", $cidr);
- $ip = $parts[0];
- $netmask = $parts[1];
- $octets = explode(".", $ip);
- foreach ($octets as $octet)
- {
- if ($octet > 255)
- {
- $return = false;
- }
- }
- if ((($netmask != "") && ($netmask > 32) && !$must_cidr) || (($netmask == ""||$netmask > 32) && $must_cidr))
- {
- $return = false;
- }
- }
- return $return;
- }
- /**
- * @author phptuts
- * @copyright 2012
- * @link http://tutorialspots.com/
- */
- function ip2cidr($ips)
- {
- $return = array();
- $num = ip2long($ips[1]) - ip2long($ips[0]) + 1;
- $bin = decbin($num);
- $chunk = str_split($bin);
- $chunk = array_reverse($chunk);
- $start = 0;
- while ($start < count($chunk))
- {
- if ($chunk[$start] != 0)
- {
- $start_ip = isset($range) ? long2ip(ip2long($range[1]) + 1) : $ips[0];
- $range = cidr2ip($start_ip . '/' . (32 - $start));
- $return[] = $start_ip . '/' . (32 - $start);
- }
- $start++;
- }
- return $return;
- }
- //----------------------------------------------------------------------------------
- $strOut = '';
- $arr = file ($fileIn);
- foreach ($arr as $str)
- {
- $range = explode (' - ', $str);
- $ips = array ($range['0'], $range['1']);
- $arrCidr = ip2cidr ($ips);
- foreach ($arrCidr as $ip)
- {
- $strOut .= $ip . "\n";
- }
- }
- file_put_contents ($fileOut, $strOut);
- echo 'OK' . "\n\n\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement