List iptables rules as append command
It's super simple:
iptables -S
gives you exactly what you ask for.
You can get similar output by using iptables-save
command:
*nat
:PREROUTING ACCEPT [381:53396]
:INPUT ACCEPT [286:22260]
:OUTPUT ACCEPT [1462:92025]
:POSTROUTING ACCEPT [1450:91003]
-A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
There are numbers and some extra info, but you can remove them by writing something like that:
iptables-save | grep -v -e "^[*:#]" -e "COMMIT" | cat -n
And the output:
1 -A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
2 -A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p udp -j MASQUERADE --to-ports 1024-65535