Wildcard iptables delete
Hello. I'm brand new to Linux, but a long time Windows administrator, so I understand the concepts, not the syntax.
I've stumbled through setting up Ubuntu Server as a NAT server. It's working great now.
My goal is to map public IPs to private IPs. However, I want to change them often through an automated fashion (SSH remote call). So, I want to run commands that will remove the previous rule and add a new rule, or update the existing rule.
For example, the original rule may be something like:
iptables -t nat -I PREROUTING -d 184.108.40.206 -j DNAT --to-destination 10.240.5.5
iptables -t nat -I POSTROUTING -s 10.240.5.5 -j SNAT --to-source 220.127.116.11
The issue I'm running into is finding out how to delete the previous entry before adding a new one. For example, let's say that 18.104.22.168 should now map to 10.240.5.6 (instead of .5).
Is there a wildcard option for the delete rule so that I don't need to know the original destination IP? i.e.
iptables -t nat -D PREROUTING -d 22.214.171.124 -j DNAT --to-destination [+]
Or, would it make sense to create a script that traverses all rules, gets the existing rule, and then deletes it?
Of course my next issue would be that I don't know any Linux scripting so it's hard to know where to start.