Created
August 7, 2023 19:03
-
-
Save alexnoz/a8ca34e11ec094b26590f255d25f79d3 to your computer and use it in GitHub Desktop.
Remove custom iptables chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!bin/bash | |
table=$1 | |
pattern=$2 | |
if [[ -z "$table" ]] || [[ -z "$pattern" ]]; then | |
echo "Usage: $0 <iptables-table-name> <iptables-chain-name-grep-pattern>" | |
exit 1 | |
fi | |
# Remove all references to the chain and all rules from the chain first | |
iptables -t "$table" -S | grep "$pattern" | grep -- '-A ' | awk '{first = $1; $1=""; print $0}' | sed 's,[][()|;*?$~{}+@%],\\&,g' | tr -d '\' | xargs -t -d '\n' -I % sh -f -c 'iptables -t '"$table"' -D %' | |
# Then remove the chain | |
iptables -t "$table" -S | grep "$pattern" | grep -- '-N ' | awk '{ print $2 }' | xargs -n1 iptables -t "$table" -X |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment