problem updating dst-address in nat rule with script

I used following script for months to update nat rule (dynamic public ip) and everything worked:

:global currentIP;

:local newIP [/ip address get [find interface="pppoe-out1"] address];

:if ($newIP != $currentIP) do={
:put "ip address $currentIP changed to $newIP";
:set currentIP $newIP;
}
/ip firewall nat set 3 dst-address=$currentIP

Yesterday I added another port (nat rule #6), but whatever I do I can't get it (script) to update dst-address.
When pasted to terminal this line immediately updates specified nat rule
/ip firewall nat set 6 dst-address=$currentIP

Original script still updates nat rule #3.
I tried replacing 3 with 6 in original script and nothing happens.
What am I missing?


:global currentIP;

:local newIP [/ip address get [find interface="pppoe-out1"] address];

:if ($newIP != $currentIP) do={
:put "ip address $currentIP changed to $newIP";
:set currentIP $newIP;
}
/ip firewall nat set 6 dst-address=$currentIP

You should not use line numbers in a script. Best to use the “find” parameter.
The new port has a new name? Both show evaluating “pppoe-out1”.
You are also storing both IP addresses in the same variable. I changed that to currentIP1.

:global currentIP1;
:local pppoePtr [/ip address find interface="pppoe-out1"];
:local newIP [/ip address get $pppoePtr address];
:local natPtr [/ip firewall nat find comment="pppoe-out1"];

:if ($newIP != $currentIP1) do={
:log info "ip address $currentIP1 changed to $newIP";
:set currentIP1 $newIP;
}

/ip firewall nat set $natPtr dst-address=$currentIP1

To check the other interface, you change “currentIP1” to “currentIP2”, and “pppoe-out1” to “pppoe-out2”.
I have not run this to check it.

ADD: Don’t use “:put” command in a script. Use “:log info” instead. Then check the log.
And my bad. I did not notice the set is not the same location. I had the “find” in “/ip address” and using it in “/ip firewall nat”. I set the comment on firewall rules to find them. Set rule 3 comment to “pppoe-out1” and set rule 6 comment to “pppoe-out2”.
Then use the above code. The coffee is working now! :smiley: