[請益] 可以幫我看看我寫的對不對嗎?

看板Perl作者 (東大)時間15年前 (2008/10/01 00:04), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
事情是這樣的,那天無聊在逛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
文章代碼(AID): #18uau1qE (Perl)