So, I have a dynamic IP address assigned, and I have a script that, when run from the terminal, works perfectly and does what it is supposed to. When I run the script from the Script window in Winbox, it does what it should. When I create a schedule to run the script, it doesn’t work, and I receive the error executing the script from scheduler failed, please check manually
If anyone has any insights, it would be appreciated.
Scheduler has policies [read, write], On Event: scriptName, I have tried placing the script code in the On Event and get the same results.
Script has policies [read, write] code is below
:global getPublicIP do={
# tag for temporary configurations
:local tag "get-public-ip-via-$interface"
# service that provides public ip address in http response
:local lookupProtocol "https"
:local lookupHostname "api.ipify.org"
# add temporary address list for lookup service
/ip firewall address-list add list=$tag address=$lookupHostname comment=$tag timeout=15s
:delay 3;
# configure temporary route to lookup service via a specific interface
:foreach p in=[/ip firewall address-list find comment=$lookupHostname dynamic=yes] do={
:local addr [/ip firewall address-list get $p address];
/ip route add gateway=$gateway dst-address=$addr comment=$tag
}
# fetch http response from lookup service
:local result [/tool fetch url="$lookupProtocol://$lookupHostname" as-value output=user]
# remove temporary address list
/ip firewall address-list remove [find where comment=$tag]
# remove temporary gateway route
/ip route remove [find where comment=$tag]
# return ip from lookup service response data
:return ($result->"data")
}
:global i 0;
:global f 0;
:global listVar "";
:put ("Fetching Current IP ...");
:log info message=("Fetching Current IP ...");
:global currentIP [$getPublicIP interface="ether1-gateway" gateway="192.168.1.1"];
:put ("Current IP: $currentIP");
:log info message=("Current IP: $currentIP");
# find firewall rules with our comment in it, and put the number of rule in a list
:put ("Locating desired firewall rules...");
:log info message=("Locating desired firewall rules...");
:foreach c in=[/ip firewall nat find] do={
:if ([:find [/ip firewall nat get $c comment] "CODE"]=0) do={
:set $f ($f + 1)
:if ([/ip firewall nat get $c dst-address]!=$currentIP) do={
:put ("Updating firewall rule #$i");
:log info ("Updating firewall rule #$i");
/ip firewall nat set $c dst-address=$currentIP;
}
}
:set $i ($i + 1);
};
:if ($f=0) do={
:put ("No firewall rules to update");
:log info ("No firewall rules to update");
}
:put ("Complete.");
:log info message=("Complete.");