アクセスを制限して国全体をブロックしたい場合があります。なんで?必要な時があるからです。
これは、スクリプトを構築するスクリプトです….
www.ipdeny.com から IP 範囲をダウンロードし、2 文字の国コードのリストを処理して、次のような bash スクリプトを作成します。
既存の iptables チェーンを削除します。
新しいチェーン「BadCountry」を作成します。
これを INPUT チェーンの先頭に追加して、ポート 80 上のすべてのものを BadCountry チェーンに渡します。
すべての IP を追加します関連する国で拒否/到達不能の BadCountry チェーンにブロックします。
必要に応じて自由に変更してください。
(ああ、パラメータ undo でスクリプトを呼び出すこともできます チェーンを削除します。)
#!/bin/bash
PARAM=${1}
if [ "${PARAM}" == "undo" ] ; then
iptables -D INPUT -p tcp -m tcp --dport 80 -j BadCountry
iptables --flush BadCountry
iptables -X BadCountry
else
echo $(date) IP Blocking GLOBAL START
#First call ourselves to undo (delete the chain)
${0} undo
#This is where the executable script that does the table update will live.
TABLESCRIPT=/root/scripts/countrytables.sh
#Change this to a folder you can write to
cd /root/ipblocks
#and delete any zone file tar/zip files
rm -f all-zones.tar.*
echo $(date) Download Countries START
wget "http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz"
tar -zxvf all-zones.tar.gz > /dev/null
echo $(date) Download Countries FINISH
echo $(date) Build Countries START
echo "#!/bin/bash" > ${TABLESCRIPT}
echo "iptables -N BadCountry" >> ${TABLESCRIPT}
echo "iptables -I INPUT -p tcp -m tcp --dport 80 -j BadCountry" >> ${TABLESCRIPT}
echo "iptables -A BadCountry -j RETURN" >> ${TABLESCRIPT}
for COUNTRY in hk cn in id kr my ph tw th vn pk ; do
awk {'print "iptables -I BadCountry -s "$1" -j REJECT --reject-with icmp-port-unreachable"'} ${COUNTRY}.zone >> ${TABLESCRIPT}
done
echo $(date) Build Countries FINISH
echo $(date) Updating iptables START
#Make our script executable
chmod 700 ${TABLESCRIPT}
#And now execute it
${TABLESCRIPT}
echo $(date) Updating iptables FINISH
fi
# Elvis Has Left The Server.