0

Is there a way to delete multiple lines in an iptables not knowing what is in my iptables?

For example, I want to delete every port forwarding from port 80 and here is the 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

Is there a way to delete these two lines in one command?

0

Here is a working script:

 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.