从bash脚本中删除所有DROP iptables规则

问题描述 投票:0回答:1

我想从bash脚本中删除所有iptables DROP规则。这是我的脚本:

#!/bin/bash

# Remove all DROPs.

######################################################################
#_____________________________________________________________________
iptables="/sbin/iptables"

######################################################################
#_____________________________________________________________________
echo "[*] Removing all DROPs ..."
IFS_OLD=$IFS
IFS=$'\n'
rule_list=$(${iptables} -S | grep 'DROP$')
for drop_rule in ${rule_list}; do
    undrop_rule=$(printf -- "${drop_rule}\n" | sed 's@^-A@-D@')
    printf "[-] ${iptables} ${undrop_rule}\n"

    ${iptables} -v ${undrop_rule}
    [ $? == 1 ] && echo "[E] Unable to delete DROP rule." && exit 1
done
IFS=$IFS_OLD

######################################################################
#_____________________________________________________________________
printf '\n\n'
${iptables} -S

######################################################################
#_____________________________________________________________________

但是输出是:

[*] Removing all DROPs ...
[-] /sbin/iptables -D INPUT -s 209.126.1.2/32 -i eth0 -j DROP
  all opt -- in * out *  0.0.0.0/0  -> 0.0.0.0/0
iptables: Bad rule (does a matching rule exist in that chain?).
[E] Unable to delete DROP rule.

为什么?

如果我手动运行命令:

/sbin/iptables -D INPUT -s 209.126.1.2/32 -i eth0 -j DROP

工作。

谢谢,BuDuS。

linux bash iptables
1个回答
0
投票

在您的脚本中,当执行IFS时,您的iptables仍设置为换行符。因此,bash无法对命令参数进行单词拆分。

一旦不再需要修改后的值,我会立即取消IFS=$IFS_OLD,这很可能是在分配给rule_list之后。

© www.soinside.com 2019 - 2024. All rights reserved.