Skip to content

Instantly share code, notes, and snippets.

@beppu
Created November 29, 2010 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beppu/720744 to your computer and use it in GitHub Desktop.
Save beppu/720744 to your computer and use it in GitHub Desktop.
Generate a list of nginx deny statements for a given country.
#!/usr/bin/perl
use strict;
use warnings;
my $country = shift;
die("Usage: block <COUNTRY>\n") unless $country;
# Examples:
# block Russia < GeoIPCountryWhois.csv > deny_russia.list
# block Poland < GeoIPCountryWhois.csv > deny_poland.list
#
# The .csv file can be downloaded from:
# http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
while (<>) {
next unless /$country/;
next unless /\.0"/;
my ($ip, $ipx, $n, $m, $cc, $country_name) = split(",");
$ip =~ s/"//g;
if ($ip =~ /\.0\.0/) {
print "deny $ip/16;\n";
} elsif ($ip =~ /\.0/) {
print "deny $ip/24;\n";
} else {
die("wtf: $_");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment