gpt4 book ai didi

linux - Bash 脚本,需要循环帮助

转载 作者:太空狗 更新时间:2023-10-29 11:03:43 24 4
gpt4 key购买 nike

目前我正在使用这个脚本来屏蔽中国的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

最佳答案

假设第一个脚本,中国的,正在做你期望的,试试这个来处理几个国家:

#!/bin/bash

COUNTRIES="cn in iq af ir ae sg hk kw kg"

ipset -N blockall hash:net

for country in $COUNTRIES; do
wget -O - http://www.ipdeny.com/ipblocks/data/countries/$country.zone 2>/dev/null | while read ip; do
ipset -A blockall $ip;
done
done


/sbin/iptables-restore < /etc/iptables/rules.v4

请注意,不需要或不使用临时文件。

如果出于任何原因需要临时文件,请使用:

#!/bin/bash

COUNTRIES="cn in iq af ir ae sg hk kw kg"
ZONEFILE=blockall.zone

rm -f $ZONEFILE

ipset -N blockall hash:net

for country in $COUNTRIES; do
wget -O - http://www.ipdeny.com/ipblocks/data/countries/$country.zone 2>/dev/null >> $ZONEFILE
done

while read ip; do
ipset -A blockall $ip;
done < $ZONEFILE

/sbin/iptables-restore < /etc/iptables/rules.v4

关于linux - Bash 脚本,需要循环帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37480375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com