달력

102022  이전 다음

  •  
  •  
  •  
  •  
  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  
  •  
  •  
  •  

CIDR to Range

:: IT/:: Programming 2014. 10. 16. 10:14

CIDR 을 Start ~ End 로 보여주는 코드를 작성했다.

서핑해서 찾아서 좀 수정했는데..

그럭저럭 잘 확인해준다...



<?php

function Usage() {

    global $argv;

    printf("Usage : php %s [CIDR]\n", $argv[0]);

    printf("ex) php %s 192.168.5.0/24\n", $argv[0]);


    exit();

}


function cidrToRange($cidr) {

    $cidr = explode('/', $cidr);

    $ptnIpAddress = "/^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/";

    if (!preg_match($ptnIpAddress, $cidr[0]) || (int)$cidr[1] < 0 || (int)$cidr[1] > 32) {

        return false;

    }


    $range = array();

    $range["start"] = long2ip(ip2long($cidr[0]) & (-1 << (32 - (int)$cidr[1])));

    $range["end"] = long2ip(ip2long($range["start"]) + pow(2, (32 - (int)$cidr[1])) - 1);

    $range["netmask"] = long2ip(-1 << (32 - (int)$cidr[1]));


    return $range;

}


$aRange = cidrToRange($argv[1]);

if (!$aRange) Usage();


foreach ($aRange as $key => $value) {

    printf("%-15s%-15s\n", $key, $value);

}

?>



Posted by zhato

댓글을 달아 주세요