Page 1 of 1

Auto Change NAT IP

Posted: Wed Jan 18, 2023 7:26 pm
by hmdslm1
Hello All,

I want my router to change nat IP (to-addresses) whenever i execute a script.

I googled and i found a script but it seems like the script is not working.

Can someone check it for me and help?
:global natAddressList;
:global currentNatAddress;
:global nextNatAddress;

:local i 0;

:set natAddressList ["10.20.20.2", "10.20.20.3", "10.20.20.4", "1020.20.4"]
:set currentNatAddress [/ip firewall nat get [find comment="NAT rule"] to-addresses]
:set i ($i + 1)
:if ($i > ( [:len $natAddressList] -1)) do={
:set i 0
}
:set nextNatAddress ($natAddressList->$i)
while ($nextNatAddress != $currentNatAddress) do={
if ([/ping $nextNatAddress count=1] = 0) do={
:set i ($i + 1)
:if ($i > ( [:len $natAddressList] -1)) do={
:set i 0
}
:set nextNatAddress ($natAddressList->$i)
} else {
:set currentNatAddress $nextNatAddress
/ip firewall nat set [find comment="NAT rule"] to-addresses=$currentNatAddress
}
}

Re: Auto Change NAT IP

Posted: Thu Jan 19, 2023 12:33 pm
by rextended
1020.20.4 is not a valid IP....

Does it seem logical to you to offer us scripts taken almost at random elsewhere and ask us to correct them?
It is better to ask directly what is needed instead of trying to fix things done with the feet by others.

This is what you ask, just put on any part of the comment on NAT rule the text "changethis":
{
/ip firewall nat
:local list    {10.20.20.2;10.20.20.3;10.20.20.4;10.20.20.5}
:local current [get [find where comment~"changethis"] to-addresses]
:local listlen [:len $list]
:local currpos [:find $list $current -1]
:local newip   0.0.0.0
:if ($currpos = ($listlen - 1)) do={:set newip ($list->0)} else={:set newip ($list->($currpos + 1))}
set [find where comment~"changethis"] to-addresses=$newip
:log info "Replaced to-addresses on NAT rule changethis from $current to $newip"
}