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
2
.Ip
子网的合并
如:
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
行左右)
,多了效率低,
耗时长。
3
.Cidr
的查询与比较
如:
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
写的高效cidr
的lib
http://www.over-yonder.net/~fullermd/projects/libcidr/
3.
Ruby
http://ipadmin.rubyforge.org/
4.linux
及Cygwin
上用的
http://www.davjam.org/~davjam/linux/cidr/index.htm
附包的源:
http://www.cpan.org