Skip to content

Instantly share code, notes, and snippets.

@eXeC64
Created April 23, 2015 14:50
Show Gist options
  • Save eXeC64/e6df7c36d9dbfeb092e3 to your computer and use it in GitHub Desktop.
Save eXeC64/e6df7c36d9dbfeb092e3 to your computer and use it in GitHub Desktop.
block-countries.sh
#!/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