Created
April 23, 2015 14:50
-
-
Save eXeC64/e6df7c36d9dbfeb092e3 to your computer and use it in GitHub Desktop.
block-countries.sh
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
#!/bin/bash | |
set -e | |
chain_name="filter_countries" | |
blacklist="af bd cn hk" | |
#------------------------------------------------------------------------------- | |
#Clear out the rules | |
iptables -F $chain_name | |
#Fetch a fresh list of ips blocks to block | |
addresses="" | |
for c in $blacklist | |
do | |
uri="http://www.ipdeny.com/ipblocks/data/aggregated/$c-aggregated.zone" | |
new_addresses=$(grep -v "^#|^$" <(wget -O - $uri)) | |
addresses=$(echo -e "$addresses\n$new_addresses") | |
done | |
#Sort and filter them | |
addresses=$(echo "$addresses" | sort -g | uniq) | |
for ipblock in $addresses | |
do | |
echo "blocking $ipblock" | |
iptables -A $chain_name -s $ipblock -j DROP | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment