如何删除 IPTABLES 的多行

如何删除 IPTABLES 的多行

有没有办法删除多行而不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

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

答案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

相关内容