记录一个一键屏蔽国外ip连接的方法
https://hostloc.com/forum.php?mo ... ;page=1#pid15505621问了没有有效的回答
以下方法成功
#!/usr/bin/env bash
# 安装依赖包
yum -y install iptables
yum -y install ipset
# 添加集合
ipset create china hash:net maxelem 65536
# 编写脚本
cat << EOF > /home/china.sh
#!/usr/bin/env bash
# 下载国内IP网段并输入到~/cn.zone文件里面
wget --no-check-certificate -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", \$4, 32-log(\$5)/log(2)) }' > /home/china.txt
# 清空china集合
ipset flush china
# 批量将国内IP网段添加进china集合
ip=\$(cat /home/china.txt)
for i in \$ip; do
ipset add china \$i
done
EOF
# 给脚本赋予可执行权限
chmod +x /home/china.sh
# 执行脚本
sh /home/china.sh
# 检查是否将国内IP网段添加进china集合当中
ipset list china
# 设置crontab定时任务,每天零点更新一次IP集合
(crontab -l ; echo "0 0 * * * /home/china.sh") | crontab -
# 配置iptables限制访问
iptables -A INPUT -m set --match-set china src -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibitedhttps://cdn.jsdelivr.net/gh/master-of-forums/master-of-forums/public/images/patch.gif 就不折腾了,,宝塔专业版一键 啊,这是防止中转被回国的。https://cdn.jsdelivr.net/gh/master-of-forums/master-of-forums/public/images/patch.gif wget --no-check-certificate -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /home/china.txt
页:
[1]