作者beakdoosan (东大)
看板Perl
标题[请益] 可以帮我看看我写的对不对吗?
时间Wed Oct 1 00:04:16 2008
事情是这样的,那天无聊在逛twnic看到各国家申请的ip网段表
就无聊想试着用iptables挡掉整个国家的网段的爽度,就写了
下面这个程式,但mask的换算的部份不晓得对不对,可否帮
我确认一下?我是perl的初学者
由於twnic是提供ip个数,所以我把个数换算成2的n次方後,假设为A
再用32-A作为mask
下面的国家连结是乱选的,没别的意思^_^
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
my $link = '
http://trace.twnic.net.tw/ipstats/ipv4net.php?ipv4_cc=CN&ipv4_country=CHINA';
my ($content, $ip, $mask, @numbers, %targetList);
$content = get($link) or die "can't get $link: $!\n";
open(F,">content.html") or die "can't open file: $!\n";
print F $content;
close(F);
open (F, "<content.html") or die "can't open: $!\n";
while(<F>) {
chomp;
s/<[^>]*>//g;
push (@numbers, $_) if (/^\d+/);
}
close(F);
for (my $i=0; $i<$#numbers; $i+=4) {
$targetList{$numbers[$i+1]} = $numbers[$i+2];
#print "$numbers[$i+1] => $numbers[$i+2]\n";
}
`iptables -F INPUT`;
while (($ip, $mask) = each (%targetList) ) {
$mask = &countmask($mask);
my $cmd = "iptables -A INPUT -s $ip/$mask -j DROP";
print "$cmd\n";
system($cmd);
}
sub countmask {
my $mask;
$_[0] = shift;
$mask = 32 - log($_[0])/log(2);
return $mask;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.31.177.174