2016-05-27 6 views
3

Zur Zeit dieses Skript Ich verwende China die IP-Adresse zu blockieren:Bash-Skript, müssen für die Schleife helfen

# 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 

Dies funktioniert gut, aber wie kann ich es mit mehreren Ländern?

habe ich versucht, diese aber es funktioniert nicht:

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 

UPDATE

Basierend auf Agnul Antwort habe ich versucht, dies:

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 

Dann chmod ich mein Skript

chmod +x /etc/block-blockall.sh

Allerdings erstellt es nicht die Datei blockall.zone oder singuläre Datei *.zone wie es sollte.

+0

Es funktioniert nicht, weil dieses '{cn, in, iq, af, ir, ae, sg, hk, kw , kg} 'es ist falsch ... Was ist die richtige Methode für Array in bash? Entschuldigung im Voraus für die Einfachheit dieser Frage – NineCattoRules

Antwort

4

das erste Skript Unter der Annahme, Porzellans ein, das tut, was Sie erwarten, versuchen Sie dieses mehrere Länder zu behandeln:

#!/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 

Notiz temporäre Datei nicht noch verwendet wird, benötigen.

Wenn aus irgendeinem Grund die temporäre Datei ist notwendig, zu verwenden:

#!/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 
0

So etwas wie

# 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 

funktionieren sollte.

+0

Ich habe versucht, aber gleicher Fehler '--2016-05-27 10: 33: 18-- http://www.ipdeny.com/ipblocks/data/countries/%7Bcn,in , iq, af, ir, ae, sg, hk, kw, kg% 7D.zone Auflösen www.ipdeny.com (www.ipdeny.com) ... 192.xxx.xxx.22 Anschluss an www.ipdeny .com (www.ipdeny.com) | 192.xxx.xxx.22 |: 80 ... verbunden. HTTP-Anfrage gesendet, wartet auf Antwort ... 404 nicht gefunden 2016-05-27 10:33:18 FEHLER 404: Nicht gefunden.' – NineCattoRules

+2

'für i in $ (Katze ...)' ist ein Antipattern. Verwenden Sie stattdessen eine While-Schleife. Außerdem sollte es "$ c" 'allein sein, und nicht' $ c.zone', da die Erweiterung '$ c' die nachstehende' .zone' hat. –

+1

Ja, ich habe das nicht wirklich versucht ;-) mein Schlechter. – agnul

Verwandte Themen