Created
February 3, 2023 09:15
-
-
Save dynax60/7352fed74ec0a090184c789ea0042530 to your computer and use it in GitHub Desktop.
Script to get subnets in CIDR notation for the selected countries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# dnf install perl-Net | |
# wget ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.inetnum.gz | |
# wget https://ftp.apnic.net/apnic/whois/apnic.db.inetnum.gz | |
# zcat ripe.db.inetnum.gz | ./subnets.pl RU BY | |
# zcat apnic.db.inetnum.gz | ./subnets.pl MN | |
use Net::CIDR qw(range2cidr); | |
use Symbol; | |
use strict; | |
use warnings; | |
my $fds = {}; | |
my @countries = @ARGV; | |
die "Invalid specified country code\n" if grep {!/^[A-Z]{2}$/} @countries; | |
foreach (@countries) { | |
$fds->{$_} = gensym; | |
my $fname = "$_.subnets"; | |
open $fds->{$_}, "> $fname" or die "Cannot open $fname for write: $!\n"; | |
} | |
undef $/; | |
++$|; | |
$_ = <STDIN>; | |
while (/inetnum:\s*(?<range>[^\n]+)\n.*?country:\s+(?<country>\w{2})/gsmx) { | |
my $cidr = range2cidr($+{range}); | |
my $country = $+{country}; | |
if (~~@countries) { | |
next unless scalar(grep { $+{country} eq $_ } @countries) > 0; | |
my $fd = $fds->{$country}; | |
print $fd "$cidr\n"; | |
} else { | |
print "$cidr\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment