【发布时间】:2016-05-30 00:09:27
【问题描述】:
目前我正在使用这个脚本来屏蔽中国的IP地址:
# Create the ipset list
ipset -N china hash:net
# remove any old list that might exist from previous runs of this script
rm cn.zone
# Pull the latest IP set for China
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone
# Add each IP address from the downloaded list into the ipset 'china'
for i in $(cat ./cn.zone ); do ipset -A china $i; done
# Restore iptables
/sbin/iptables-restore < /etc/iptables/rules.v4
这很好用,但我如何在多个国家/地区使用它?
我试过了,但它不起作用:
ipset -N blockall hash:net
rm blockall.zone
for i in $(wget -P . http://www.ipdeny.com/ipblocks/data/countries/{cn,in,iq,af,ir,ae,sg,hk,kw,kg}.zone);
do ipset -A blockall $i; done
/sbin/iptables-restore < /etc/iptables/rules.v4
更新
根据 Agnul 的回答,我尝试了这个:
rm blockall.zone
# pull files for each country
wget -P . http://www.ipdeny.com/ipblocks/data/countries/{cn,in,iq,af,ir,ae,sg,hk,kw,kg}.zone
# for each country file
for c in *.zone; do
#for each line in country
while read i; do
ipset -A blockall $i;
done <"$c"
done
然后我chmod我的脚本
chmod +x /etc/block-blockall.sh
但是,它并没有按照应有的方式创建文件 blockall.zone 或单个文件 *.zone。
【问题讨论】:
-
它不起作用,因为这个
{cn,in,iq,af,ir,ae,sg,hk,kw,kg}是错误的......bash 中数组的正确方法是什么?为这个问题的简单性提前道歉