Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107566
  • 博文数量: 38
  • 博客积分: 1520
  • 博客等级: 上尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-20 11:12
文章分类

全部博文(38)

文章存档

2012年(4)

2011年(1)

2010年(7)

2009年(21)

2008年(1)

2007年(4)

我的朋友

分类: LINUX

2008-03-04 13:07:59

Cidr 计算的几种方式

 

一.             使用Net::CIDR

1.       计算子网掩码

如:192.168.1.0-192.168.4.255

源码:

use Net::CIDR;

 

    use Net::CIDR ':all';

open(MYFILE,"ip")||die "$!";

while($ip=){

    print join("\n",

          Net::CIDR::range2cidr($ip))

               . "\n";

 }   

  close(MYFILE);

结果:

192.168.1.0/24

192.168.2.0/23

192.168.4.0/24

2Ip子网的合并

如:

192.168.0.0/24

192.168.1.0/24

192.168.2.0/24

192.168.3.0/24

192.168.4.0/24

源码:

use Net::CIDR;

 

    use Net::CIDR ':all';

     

open(MYFILE,"test1")||die "$!";

#@list=("");

while($ip=){

  @list=Net::CIDR::cidradd($ip, @list);

 }  

  print join("\n", @list) . "\n";

  close(MYFILE);  

结果:

192.168.0.0/22

192.168.4.0/24

注:这种合并方式对少量的ip段还成(1000行左右),多了效率低,耗时长。

3Cidr的查询与比较

如:

61.191.190.236

220.231.31.128

192.168.1.3

源码:

use Net::CIDR;

 

    use Net::CIDR ':all';

open(MYFILE,"ip")||die "$!";

@chn=qw(

        61.29.224.0/19

      61.28.0.0/19

      61.190.0.0/15

 );

 @cnc=qw(

        220.231.31.0/24

      211.147.6.3/32

      202.106.0.20/32

      );

 

while($ip=){

              if(Net::CIDR::cidrlookup($ip, @chn)==1)

              {

                     printf "CHN->success::$ip";

              }

              elsif(Net::CIDR::cidrlookup($ip, @cnc)==1)

              {

                     printf "CNC->success::$ip";

              }

              else

              {

                      $num++ ;

                      printf "Top->Failed::$ip";

                     # printf "$ip";

              }

 } 

 printf $num;

 close(MYFILE);        

结果:

CHN->success::61.191.190.236

CNC->success::220.231.31.128

Top->Failed::192.168.1.3

注:也可以查单个ip段,这种方式效率低,耗时长。

4.计算子网范围

如:

192.68.0.0/16

源码:

print join("\n", Net::CIDR::cidr2range("192.68.0.0/16")) . "\n";

结果:

192.68.0.0-192.68.255.255

二.使用Net::IP::Match::Regexp计算ip配置较高效

源码:

#!/usr/bin/perl

use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip );

open(MYFILE,"ldns")||die "$!";

    my $ip; 

    my $num;

    my $cn = create_iprange_regexp(

        qw(

116.1.0.0/16

116.112.0.0/14

116.116.0.0/15

116.2.0.0/15

116.207.0.0/16

      )

     );               

       

    my $nocn = create_iprange_regexp(

        qw(

 10.0.0.0/8

 116.0.24.0/21

 116.118.0.0/17

         )

     );       

       

    my $chn = create_iprange_regexp(

        qw(

 116.16.0.0/12

 116.252.0.0/15

 117.22.0.0/15

 121.36.0.0/16

 121.8.0.0/13

 122.8.182.0/23

 123.52.0.0/14

 124.114.0.0/15

 124.116.0.0/16

 124.224.0.0/16

         )

     );

    

     my $cnc = create_iprange_regexp(

        qw(

116.13.0.0/16

116.56.0.0/15

118.132.0.0/14

121.100.128.0/19

          )

     );

$num=0;

  while($ip=){

         if (match_ip($ip, $chn)) {

       printf "CHN->success::$ip";

     }

     elsif (match_ip($ip, $cnc)) {

       printf "CNC->success::$ip";

     }

     elsif (match_ip($ip, $nocn)) {

       printf "NOCN->success::$ip";    

    }

    elsif (match_ip($ip, $cn)) {

       printf "CN->success::$ip";  

    }

     else{

          $num++ ;

          printf "Top->Failed::$ip";

     # printf "$ip";

    }

   

}

    

 printf $num;

 close(MYFILE);   

三.             其它

1. 入数据库的算法

源码:

#!/usr/bin/perl

 

open(FD,"ip.txt")||die "$!";

 

while($line=){

         my($start_ip, $end_ip,$cc,$cn)=split(/\s+/,$line);

              $num1=numToipnum($start_ip);

              $num2=numToipnum($end_ip);

  

 printf"INSERT INTO csv (start_ip,end_ip,start,end,cc,cn ) VALUES ('$start_ip','$end_ip','$num1','$num2','$cc','$cn')\;\n";

 

}

 

close(FD);

 

sub numToipnum {

       my ($ip)=@_;

  my ($w,$x,$y,$z) =split(/\./,$ip);

  my $ipnum = 16777216*$w + 65536*$x + 256*$y + $z ;

  return $ipnum;

}

注:调用的时候用sql语句,比较ip的大小范围

2. c写的高效cidrlib

~fullermd/projects/libcidr/

3. Ruby

4.linuxCygwin上用的

~davjam/linux/cidr/index.htm

 

 

 

附包的源:

阅读(4608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~