Say there are several iptables scripts (run at boot time), all of which run something like iptables -A ... to add rules. I'm thinking this could be improved, turning all those shell scripts into text files generated by iptables-save.
But I must be doing something wrong, trying to read all those rulesets. The script run at boot time would loop through those files and read them using iptables-restore. Of course with -n or --noflush. This works for some rules (stored in the default chains) but not for most of my rules which are in other chains. Below is an example of 2 rulesets that flush each other (reading set a, check; reading set b, check but set a is gone).
How would you read a bunch of iptables rulesets?
Example:
$ cat fake1-a.rules
*nat
:PREROUTING ACCEPT [7:997]
:INPUT ACCEPT [7:997]
:OUTPUT ACCEPT [28:1810]
:POSTROUTING ACCEPT [28:1810]
COMMIT
*mangle
:PREROUTING ACCEPT [344:84621]
:INPUT ACCEPT [344:84621]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [296:37971]
:POSTROUTING ACCEPT [296:37971]
COMMIT
*filter
:INPUT ACCEPT [102:26513]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [89:10767]
:TESTCHAIN - [0:0]
-A TESTCHAIN -p tcp -m tcp --dport 12345 -j DROP
COMMIT
$ cat fake1-b.rules
*nat
:PREROUTING ACCEPT [7:997]
:INPUT ACCEPT [7:997]
:OUTPUT ACCEPT [28:1810]
:POSTROUTING ACCEPT [28:1810]
COMMIT
*mangle
:PREROUTING ACCEPT [344:84621]
:INPUT ACCEPT [344:84621]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [296:37971]
:POSTROUTING ACCEPT [296:37971]
COMMIT
*filter
:INPUT ACCEPT [102:26513]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [89:10767]
:TESTCHAIN - [0:0]
-A TESTCHAIN -p tcp -m tcp --dport 54321 -j DROP
COMMIT
# cat fake1-a.rules | iptables-restore --noflush
# iptables -nL | grep DROP
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:12345
# cat fake1-b.rules | iptables-restore --noflush
# iptables -nL | grep DROP
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:54321