Updating NAT rule on interface IP address change (DHCP client)
Posted: Tue Oct 11, 2022 1:49 pm
Hi All,
The need is to have the NAT rule allowing access to DMZ server.
Current implementation uses as selector: in-interface-list=WAN
This works only for traffic arriving from outside and better would be to have selector by IP address of Mikrotik WAN interface.
After reading it seems like there's no easy way outside of going for scripting:
# https://wiki.mikrotik.com/wiki/Manual:S ... ve_changed
The second problem is that on that external interface with IP set by DHCP server, there's another static local IP. I am not sure how to modify the script to select only the DHCP IP and not the static one? (if there would be sort of "grep -v" I could grep out the unwanted one).
With that said, am not sure what's the best way to amend existing NAT rule.
Looking at syntax, the only selector for rule to be amended is rule#, which might change over the time as NAT rules would be updated and all of the sudden script could be modifying wrong rule
What is better way to do it? The best would be is there would be a built-in list of objects, like IP addresses on interfaces and easy to use.
Looking at it for the second time, probably better will be to use Address List, which would be updated by script instead of the rule - though same worry comes - how to select the right one, as the Number is not the best selector.
Regardless of the worry of amending the rule, feeling is that the action should be added straight after
Please share if there are better ways to achieve this - seems like a common use case hence am finding surprising that it is that difficult.
The need is to have the NAT rule allowing access to DMZ server.
Current implementation uses as selector: in-interface-list=WAN
Code: Select all
chain=dstnat action=dst-nat to-addresses=192.168.1.1 to-ports=10443 protocol=tcp [b]in-interface-list=WAN[/b] dst-port=443 log=no
log-prefix=""
After reading it seems like there's no easy way outside of going for scripting:
# https://wiki.mikrotik.com/wiki/Manual:S ... ve_changed
Code: Select all
:global currentIP;
:local newIP [/ip address get [find interface="ether1"] address];
:if ($newIP != $currentIP) do={
:put "ip address $currentIP changed to $newIP";
:set currentIP $newIP;
}
With that said, am not sure what's the best way to amend existing NAT rule.
Looking at syntax, the only selector for rule to be amended is rule#, which might change over the time as NAT rules would be updated and all of the sudden script could be modifying wrong rule
Code: Select all
/ip firewall nat edit number=....
Looking at it for the second time, probably better will be to use Address List, which would be updated by script instead of the rule - though same worry comes - how to select the right one, as the Number is not the best selector.
Regardless of the worry of amending the rule, feeling is that the action should be added straight after
Code: Select all
:set currentIP $newIP;