Created
May 29, 2017 14:32
-
-
Save wallneradam/1e13c7bca1c7c984ee543a4e97089cf3 to your computer and use it in GitHub Desktop.
Iptables rules deduplication script
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/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome. Thank you.