用 iptables 屏蔽来自某个国家的 IP
首先去 http://www.ipdeny.com/ipblocks/ 下载对应的国家IP地址端

写个脚本循环导入即可。

#!/bin/bash
# Block traffic from a specific country
# written by vpsee.com

COUNTRY = "cn"
IPTABLES = /sbin/iptables
EGREP = /bin/egrep

if [ "$(id -u)" != "0" ]; then
   echo "you must be root" 1>&2
   exit 1
fi

resetrules() {
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -X
}

resetrules

for c in $COUNTRY
do
        country_file = $c.zone

        IPS = $($EGREP -v "^#|^$" $country_file)
        for ip in $IPS
        do
           echo "blocking $ip"
           $IPTABLES -A INPUT -s $ip -j DROP
        done
done

exit 0



游客 | 登入