本文发表在 rolia.net 枫下论坛<pre>
===============
#!/usr/bin/perl
while (<>) {
$low = limit(1, getipmask($_));
$high = limit(0, getipmask($_));
open(MYIPS,"./ips.txt") || die "open err\n";
while (<MYIPS>) {
if ($low >= limit(0, getipmask($_)) && $high <= limit(1, getipmask($_))) {
print "Found: ", $_, "\n";
}
}
close MYIPS;
}
sub limit {
($highlow, $mask, @octet) = @_;
my $ip = 0;
for ($i=0; $i<4; $i++) {
$ip += $octet[$i]*256**(3-$i);
}
$ip = $ip & (2 ** 32 - 2 ** (32-$mask));
if ($highlow == 1) {
return $ip + 2 ** (32-$mask) - 1;
} else {
return $ip;
}
}
sub getipmask {
chomp;
if ($_ =~ /\//) {
($myip, $mymask) = split (/\//, $_);
} else {
$myip = $_;
$mymask = 32;
}
return($mymask, split (/\./, $myip));
}
===============
</pre>更多精彩文章及讨论,请光临枫下论坛 rolia.net
===============
#!/usr/bin/perl
while (<>) {
$low = limit(1, getipmask($_));
$high = limit(0, getipmask($_));
open(MYIPS,"./ips.txt") || die "open err\n";
while (<MYIPS>) {
if ($low >= limit(0, getipmask($_)) && $high <= limit(1, getipmask($_))) {
print "Found: ", $_, "\n";
}
}
close MYIPS;
}
sub limit {
($highlow, $mask, @octet) = @_;
my $ip = 0;
for ($i=0; $i<4; $i++) {
$ip += $octet[$i]*256**(3-$i);
}
$ip = $ip & (2 ** 32 - 2 ** (32-$mask));
if ($highlow == 1) {
return $ip + 2 ** (32-$mask) - 1;
} else {
return $ip;
}
}
sub getipmask {
chomp;
if ($_ =~ /\//) {
($myip, $mymask) = split (/\//, $_);
} else {
$myip = $_;
$mymask = 32;
}
return($mymask, split (/\./, $myip));
}
===============
</pre>更多精彩文章及讨论,请光临枫下论坛 rolia.net