« 安装 MaxMind GeoIP、GeoIP City Lite 为 awstats 配置 IP 位置国家城市统计信息 | Main | 关于嵌套 js 代码方式收集网站访问信息的一个 cookie 隐私问题 »
September 5, 2006
写了一段 perl 处理 ip range 的代码
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。 https://windtear.net/archives/2006/09/05/001076.html http://windtear.net/archives/2006/09/05/001076.html 写了一段 perl 处理 ip range 的代码 需求就是做如下 ip range 转换: 192.168.1.1-3,192.168.2.4-6,192.168.3.8 192.168.1.1,192.168.1.2,192.168.1.3,192.168.2.4,192.168.2.5,192.168.2.6,192.168.3.8 现成的 module 模组有不少IP处理的 反正趁有时间就赶快写出来了 变量命名没考虑 直接 wt 了 (wt = windtear) $min $max 的大小判断就略了 虽然细节决定成败 不过还是能省就省吧 # add by windtear start my $wtaddrlist=""; if ($addrlist =~ /,/ && $addrlist =~ /\d+\.\d+\.\d+\.\d+-\d+/) { my @wthostlist=split(',',$addrlist); foreach my $wthost (@wthostlist) { if ($wthost =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)/) { my $ipfirst=$1.".".$2.".".$3."."; my $min=$4; my $max=$5; for my $wtiplast ($min..$max) { $wtaddrlist .=",".$ipfirst.$wtiplast; } } else { $wtaddrlist .=",".$wthost; } } $addrlist=substr($wtaddrlist,1,length($wtaddrlist)-1); } elsif ($addrlist =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)/) { my $ipfirst=$1.".".$2.".".$3."."; my $min=$4; my $max=$5; for my $wtiplast ($min..$max) { $wtaddrlist .=",".$ipfirst.$wtiplast; } $addrlist=substr($wtaddrlist,1,length($wtaddrlist)-1); } # add by windtear end |
Posted by windtear at September 5, 2006 8:26 PM