【问题标题】:Remove all DROP iptables rules from bash script从 bash 脚本中删除所有 DROP iptables 规则
【发布时间】:2020-05-25 06:13:59
【问题描述】:

我想从 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】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-30
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2016-01-09
      • 1970-01-01
      • 2022-10-17
      相关资源
      最近更新 更多