#!/usr/bin/perl
# makeIPBlockList.pl
# http://iplist.wave.prohosting.com/ のIPリストから接続をブロックするIPアドレスのリストを作成する。
use strict;
use warnings;
use utf8;
use Encode;
use Net::IP;
my $fileNameIn = "cn.txt";
my $fileNameOut = "cn_block.txt";
binmode( STDIN, ":encoding(CP932)" );
binmode( STDOUT, ":encoding(CP932)" );
binmode( STDERR, ":encoding(CP932)" );
$| = 1;
open( my $fin, "<:utf8", encode( 'CP932', $fileNameIn ) ) or die( "$fileNameIn: $!\n" );
my @body = <$fin>;
close( $fin );
chomp( @body );
open( my $fout, ">:utf8", encode( 'CP932', $fileNameOut ) ) or die( "$fileNameOut: $!\n" );
my $max = scalar( @body );
my $count = 0;
while( @body > 0 ){
printf STDERR ( "%d/%d\r", ++$count, $max );
my $line = shift( @body );
if ( $line =~ /[^:]+:(\d+\.\d+\.\d+\.\d+)-(\d+\.\d+\.\d+\.\d+)/ ){
my $ip = new Net::IP( "$1-$2" ) or die( Net::IP::Error() );
foreach ( $ip->find_prefixes() ){
/([^\/]+)\/([^\/]+)/;
printf $fout ( "%s/%s\n", $1, makeMask( $2 ) );
}
}
}
close( $fout );
sub makeMask
{
my( $x ) = @_;
my $ret = '';
my $b = "1";
while( $x > 0 ){
if ( $x & 1 ){
$ret .= $b;
}
$b .= $b;
$x >>= 1;
}
return join( '.', unpack( 'C*', pack( 'B32', substr( $ret . "00000000000000000000000000000000", 0, 32 ) ) ) );
}
# EOF
CN:58.14.0.0-58.25.255.255
CN:58.30.0.0-58.63.255.255
CN:58.66.0.0-58.67.255.255
CN:58.68.128.0-58.68.255.255
CN:58.82.0.0-58.83.255.255
CN:58.87.64.0-58.87.127.255
CN:58.99.128.0-58.101.255.255
CN:58.116.0.0-58.119.255.255
CN:58.128.0.0-58.135.255.255
CN:58.144.0.0-58.144.255.255
58.14.0.0/255.254.0.0
58.16.0.0/255.248.0.0
58.24.0.0/255.254.0.0
58.30.0.0/255.254.0.0
58.32.0.0/255.224.0.0
58.66.0.0/255.254.0.0
58.68.128.0/255.255.128.0
58.82.0.0/255.254.0.0
58.87.64.0/255.255.192.0
58.99.128.0/255.255.128.0
58.100.0.0/255.254.0.0
58.116.0.0/255.252.0.0
58.128.0.0/255.248.0.0
58.144.0.0/255.255.0.0
#!/usr/bin/perl
# makeIPBlockList2.pl
# http://nami.jp/ipv4bycc/mask.txt からアクセス禁止国のIPアドレスを抽出
use strict;
use warnings;
use utf8;
use Encode;
#my $charset_console = 'UTF-8';
my $charset_console = 'CP932';
my $charset_file = 'UTF-8';
binmode( STDIN, ":encoding($charset_console)" );
binmode( STDOUT, ":encoding($charset_console)" );
binmode( STDERR, ":encoding($charset_console)" );
my $fileNameIn = 'mask.txt';
my $fileNameOut = 'block_list.txt';
my @countries = qw( CN KR RU BR );
my $block_countries = '^(' . join( "|", map{ quotemeta($_); } @countries ) . ')$';
open( my $fin, "<:encoding($charset_file)", encode( $charset_console, $fileNameIn ) )
or die( "$fileNameIn: $!\n" );
open( my $fout, ">:encoding($charset_file)", encode( $charset_console, $fileNameOut ) )
or die( "$fileNameOut: $!\n" );
foreach my $line ( <$fin> ){
my( $country, $ip ) = split( /\s/, $line );
if ( $country =~ /$block_countries/i ){
#$ip =~ s{/}{,};
printf $fout ( "%s\n", $ip );
}
}
close( $fin );
close( $fout );
# EOF