Created
November 29, 2010 22:20
-
-
Save beppu/720744 to your computer and use it in GitHub Desktop.
Generate a list of nginx deny statements for a given country.
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 | |
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