开发者

How can I skip some elements in a for loop?

开发者 https://www.devze.com 2023-02-25 07:48 出处:网络
#!/bin/sh BLOCKDB=\"/opt/ip.blocked\" IPS=$(grep -Ev \"^#\" $BLOCKDB) for i in $IPS do sudo iptables -A INPUT -s $i -j DROP
#!/bin/sh
BLOCKDB="/opt/ip.blocked"
IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
do
  sudo iptables -A INPUT -s $i -j DROP
  sudo iptables -A OUTPUT -d $i -j DROP
done

I need an addi开发者_StackOverflow社区tional statement inside the loop, to check if an IP address is inside the iptables list and if it's already inside, then continue the loop.

The checking statement would be this:

iptables -L INPUT -v -n | grep $i

How can I put this in here?


So, what's the question? How to continue or break?

iptables -L INPUT -v -n | grep $i && continnue

for instance.


try the following:

for i in $IPS
do
    sudo iptables -L INPUT -v -n | grep $i
    if [ $? -eq 0 ]
    then
        sudo iptables -A INPUT -s $i -j DROP
        sudo iptables -A OUTPUT -d $i -j DROP
    fi
done
0

精彩评论

暂无评论...
验证码 换一张
取 消