scriptingmikrotik

Mikrotik Script runs perfectly in terminal but doesn't run when placed in scripts


I am trying to add a script to the list in order to schedule it to run it on interval base but it's not working when I place it in scripts and works perfectly when I manually execute the script in Terminal in Mikrotik. I have tried both these version of script as :

The \n /r/ Script Version:

{\r\
\n:global NSM2 [/ip neighbor find mac-address="68:72:51:34:AA:03"];/r/
\n:foreach i in=$NSM2 do={/r/
\n:global nsm2tx [/ip neighbor get $i address];/r/
\n}/r/
\n/ip firewall nat set 11 to-address=$nsm2tx/r/
\n:global NSM2 [/ip neighbor find mac-address="68:72:51:32:11:60"];/r/
\n:foreach i in=$NSM2 do={/r/
\n:global nsm2rx [/ip neighbor get $i address];/r/
\n}/r/
\n/ip firewall nat set 10 to-addresses=$nsm2rx/r/
\n}

The Normal Version :

:global NSM2 [/ip neighbor find mac-address="68:72:51:34:AA:03"];
:foreach i in=$NSM2 do={
:global nsm2tx [/ip neighbor get $i address];
}
/ip firewall nat set 11 to-address=$nsm2tx

:global NSM2 [/ip neighbor find mac-address="68:72:51:32:11:60"];
:foreach i in=$NSM2 do={
:global nsm2rx [/ip neighbor get $i address];
}
/ip firewall nat set 10 to-addresses=$nsm2rx

But none of these works when placed in scripts and then click on run button or when schedule runs..!


Solution

  • The idea behind your script is correct but the syntax wasn't; Mikrotik scripting language is very picky, so i reviewed it, here is a working version:

    :local nsm2tx [/ip neighbor get [find mac-address="68:72:51:34:AA:03"] address];
    /ip firewall nat set 10 to-addresses="$nsm2tx"