Skip to content

Instantly share code, notes, and snippets.

@yteraoka
Last active December 19, 2015 22:29
Show Gist options
  • Save yteraoka/6028063 to your computer and use it in GitHub Desktop.
Save yteraoka/6028063 to your computer and use it in GitHub Desktop.
APNIC の IP Address リストから日本に割り当てられている CIDR のリストを抽出する

使い方

curl -s http://ftp.apnic.net/stats/apnic/delegated-apnic-latest | perl jpaddr.pl
#!/usr/bin/perl
#
# http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
#
#
use strict;
use warnings;
print "#" x 78 ."\n";
print "# \$Id\$\n";
print "#\n";
print "# http://ftp.apnic.net/stats/apnic/delegated-apnic-latest\n";
print "#\n";
while (my $line = <STDIN>) {
next if ($line =~ /^#/);
$line =~ s/[\r\n]+$//;
my ($registry, $cc, $type, $start, $value, $date, $status)
= split(/\|/, $line);
if ($registry ne "apnic") {
print "#\n";
print "# $line\n" if ($cc eq "apnic");
print "#\n";
print "#" x 78 ."\n";
}
next if ($cc ne "JP");
next if ($type ne "ipv4");
printf "%s/%d\n", $start, num2cidr($value);
}
sub num2cidr {
my ($num) = @_;
return 32 - log($num)/log(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment