All pastes #969996 Raw Edit

IP 2 Country quick lookup.

public text v1 · immutable
·published 2008-04-03 20:13 UTC
rendered paste body
#!/usr/bin/perl

#Quickly written by Deltaray with the help from kind people on the #perl channel.

use Tie::NetAddr::IP;

my %netblocks;
tie %netblocks, Tie::NetAddr::IP;

$blocklist = '/home/mkrenz/netblocks2/biglist';
open(BLOCKLIST, $blocklist) || die();

# Assumes '<block> <country>' format to the file.
while (<BLOCKLIST>) {
    chomp($_);
    $line = $_;
    next if ($line =~ m/^#/);
    ($block, $country) = split(/\s+/, $line);

    $netblocks{$block} = $country;
}
close(BLOCKLIST);

print "Ready\n";

while (<STDIN>) {
    chomp($_);
    $ip = $_;
    $return = $netblocks{$ip};
    if ($return) {
        print "$return\n";
    } else {
        print "UNKNOWN\n";
    }
}

untie %netblocks;