Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Created August 7, 2023 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexnoz/a8ca34e11ec094b26590f255d25f79d3 to your computer and use it in GitHub Desktop.
Save alexnoz/a8ca34e11ec094b26590f255d25f79d3 to your computer and use it in GitHub Desktop.
Remove custom iptables chain
#!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