开发者

How can I skip some elements in a for loop?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜