Debian Codegraph @

tools/get-country-zones.pl

raw
1 #!/usr/bin/perl
2 #
3 # Author: Paul Armstrong
4 #
5 # Downloads maps of countries to CIDR netblocks for the world and then turns
6 # them into definition files usable by Capirca
7
8 use strict;
9 use warnings;
10 use File::Find;
11
12 my @files;
13 my $destination = '../def/';
14 my $extension = '.net';
15
16 system("wget http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz")
17 == 0 or die "Unable to get all-zones.tar.gz: $?\n";
18
19 system("tar -zxf all-zones.tar.gz") == 0
20 or die "Unable to untar all-zones.tar.gz: $?\n";
21
22 # We don't need these lying around
23 unlink("Copyrights.txt");
24 unlink("MD5SUM");
25 unlink("all-zones.tar.gz");
26
27 sub zone_files
28 {
29 push @files, $File::Find::name if(/\.zone$/i);
30 }
31
32 find(\&zone_files, $ENV{PWD});
33
34 for my $file (@files)
35 {
36 if($file =~ /^.*\/([a-z]{2})\.zone/)
37 {
38 my $country = $1;
39 my $new_name = "$destination$country$extension";
40 my $country_uc = uc($country);
41 die "$file is zero bytes\n" if(!-s $file);
42 open(OLDFILE, $file) or die "Unable to open $file: $!\n";
43 open(NEWFILE, ">$new_name")
44 or die "Unable to open $new_name: $!\n";
45 while(<OLDFILE>)
46 {
47 chomp;
48 if ($. == 1)
49 {
50 print NEWFILE "${country_uc}_NETBLOCKS = $_\n"
51 or die "Unable to print to $new_name: $!\n";
52 }
53 else
54 {
55 print NEWFILE " $_\n"
56 or die "Unable to print to $new_name: $!\n";
57 }
58 }
59 close(NEWFILE) or die "$new_name didn't close properly: $!\n";
60 close(OLDFILE);
61 die "$new_name is zero bytes\n" if(!-s $new_name);
62 unlink($file); # clean up the originals.
63 }
64 }