less iptables-remove-duplicate-rules.md
iptables: remove duplicate rules
Potrebbe capitare che le regole che gestite con iptables possano diventare improvvisamente centinaia. Alcune di queste però, potrebbe essere dei semplici duplicati.
Lo strumento che corre in aiuto in maniera efficace è certamente awk. Inserito in uno script diventa super.
/sbin/iptables-save | awk '!COMMIT||!x[$0]++'
Inseriamolo in uno script che faccia il lavoro per come dovrebbe essere fatto:
#!/bin/sh
/sbin/iptables-save | awk '!COMMIT||!x[$0]++' > /tmp/tmp_iptables.conf
uniq /tmp/tmp_iptables.conf > /tmp/iptables.conf
echo "Stopping firewall and allowing everyone..."
iptables -F
iptables -X
iptables -t raw -F
iptables -t raw -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
echo "Reactivating firewall ..."
/sbin/iptables-restore < /tmp/iptables.conf
echo "All done!"
if [ -f /tmp/iptables.conf ] ; then /bin/rm -f /tmp/iptables.conf ; fi
if [ -f /tmp/tmp_iptables.conf ] ; then /bin/rm -f /tmp/tmp_iptables.conf ; fi
exit 0
Facile vero?