【问题标题】:How to delete multiple lines of the IPTABLES如何删除多行 IPTABLES
【发布时间】:2019-03-23 08:06:32
【问题描述】:

有没有办法删除iptables 中的多行而不知道我的iptables 中有什么?

例如,我想删除端口 80 的所有端口转发,这里是 iptables

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8080
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:https redir ports 8443

有没有办法在一个命令中删除这两行?

【问题讨论】:

  • 你试过cat iptables的一些组合吗? grep(针对端口 80 的东西)> iptables ?
  • 我找到了办法。但是我总是遇到这个错误:/bin/sh: 527: [[: not found。你知道这是从哪里来的吗?
  • 为此我需要知道你在做什么。
  • 感谢您的帮助。我终于找到了我的错误的根源:)

标签: linux networking iptables portforwarding


【解决方案1】:

这是一个工作脚本:

 iptables -t nat --list-rules PREROUTING | while IFS='' read -r line || [[ -n "$line" ]]; do
  RULE443=$(echo $line | grep "REDIRECT --to-ports 8443")
  RULE80=$(echo $line | grep "REDIRECT --to-ports 8080")
  if [ "$RULE80" != '' ]
  then
    PORT80=$(echo $RULE80 | grep -o '\-\-dport.*' | cut -d' ' -f2)
    iptables -t nat -D PREROUTING -p tcp --dport $PORT80 -j REDIRECT --to-port 8080
  elif [ "$RULE443" != '' ]
  then
    PORT443=$(echo $RULE443 | grep -o '\-\-dport.*' | cut -d' ' -f2)
    iptables -t nat -D PREROUTING -p tcp --dport $PORT443 -j REDIRECT --to-port 8443
  fi
done

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2016-01-09
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多