Skip to content

Instantly share code, notes, and snippets.

@wallneradam
Created May 29, 2017 14:32
Show Gist options
  • Select an option

  • Save wallneradam/1e13c7bca1c7c984ee543a4e97089cf3 to your computer and use it in GitHub Desktop.

Select an option

Save wallneradam/1e13c7bca1c7c984ee543a4e97089cf3 to your computer and use it in GitHub Desktop.
Iptables rules deduplication script
#!/bin/sh
ipt="iptables -w"
dedup() {
iptables-save | sed -n "/$1/,/COMMIT/p" | grep "^-" | sort | uniq -dc | while read l
do
c=$(echo "$l" | sed "s|^[ ]*\([0-9]*\).*$|\1|")
rule=$(echo "$l" | sed "s|^[ ]*[0-9]* -A\(.*\)$|-t $1 -D\1|")
while [ ${c} -gt 1 ]; do
echo "iptables $rule"
eval "${ipt} ${rule}"
c=$((c-1))
done
done
}
dedup "filter"
dedup "nat"
dedup "mangle"
@NeySlim
Copy link
Copy Markdown

NeySlim commented Nov 28, 2017

Awesome. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment