Problem with scripts executing from scheduler - global vars

Hi
I’m pulling my hair out here :confused:

I am running a script to update Unotelly DNS, based on some of the dynamic DNS scripts I’ve seen around.
My problem is that global variables set in the script somehow don’t seem to persist across executions:

:global previousIPUno
:local userhash "censored"


# Change to the name of interface that gets the dynamic IP address
:local inetinterface "isp"

#------------------------------------------------------------------------------------
# No more changes need


:if ([/interface get $inetinterface value-name=running]) do={
# Get the current IP on the interface
   :local currentIP [/ip address get [find interface="$inetinterface" disabled=no] address]

# Strip the net mask off the IP address
   :for i from=( [:len $currentIP] - 1) to=0 do={
       :if ( [:pick $currentIP $i] = "/") do={ 
           :set currentIP [:pick $currentIP 0 $i]
       } 
   }

   :if ($currentIP != $previousIPUno) do={
       :log info "Unotelly: Current IP $currentIP is not equal to previous IP $previousIPUno, update needed"
       :set previousIPUno $currentIP

# The update URL. Note the "\3F" is hex for question mark (?). Required since ? is a special character in commands.
       :local url "http://api.unotelly.com/api/v1/network/update_by_hash_api\3Fuser_hash=$userhash"
       :log info "Unotelly: Sending update"
           /tool fetch url=($url)
           :log info "Unotelly: Updated with IP $currentIP"
       }
   }  else={
       :log info "Unotelly: Previous IP $previousIPUno is equal to current IP, no update needed"
   }
} else={
   :log info "Unotelly: $inetinterface is not currently running, so therefore will not update."
}

:log info [/system script environment print]

The script runs from the scheduler:

20:21:03 script,info Unotelly: Current IP 105.233.57.218 is not equal to previous IP , update needed
20:21:03 script,info Unotelly: Sending update
20:21:04 info fetch: file “update_by_hash_api?user_hash=” downloaded
20:21:04 script,info Unotelly: Updated with IP 105.233.57.218
20:22:03 script,info Unotelly: Current IP 105.233.57.218 is not equal to previous IP , update needed
20:22:03 script,info Unotelly: Sending update
20:22:04 info fetch: file “update_by_hash_api?user_hash=” downloaded
20:22:04 script,info Unotelly: Updated with IP 105.233.57.218
20:23:03 script,info Unotelly: Current IP 105.233.57.218 is not equal to previous IP , update needed
20:23:03 script,info Unotelly: Sending update
20:23:04 info fetch: file “update_by_hash_api?user_hash=” downloaded
20:23:04 script,info Unotelly: Updated with IP 105.233.57.218
20:24:03 script,info Unotelly: Current IP 105.233.57.218 is not equal to previous IP , update needed
20:24:03 script,info Unotelly: Sending update
20:24:04 info fetch: file “update_by_hash_api?user_hash=” downloaded
20:24:04 script,info Unotelly: Updated with IP 105.233.57.218
20:25:03 script,info Unotelly: Current IP 105.233.57.218 is not equal to previous IP , update needed
20:25:03 script,info Unotelly: Sending update
20:25:04 info fetch: file “update_by_hash_api?user_hash=” downloaded
20:25:04 script,info Unotelly: Updated with IP 105.233.57.218


As you can see, it’s not remembering the $previousIPUno

RouterOS v6.19 on a 2011UiAS-2HnD

Any hints please?

Thanks :slight_smile:

try changing this:

:set previousIPUno $currentIP

to this:

:global previousIPUno $currentIP

Check if you have “write” rights for scripts set. It is crucial for updating global vars.

You have a curly bracket to much, remove it and it should work.

Change:

           :log info "Unotelly: Updated with IP $currentIP"
       }
   }  else={

to:

           :log info "Unotelly: Updated with IP $currentIP"
   }  else={

One more simple tip:
To check if script syntax is correct go to /system script and then do print in the terminal.
If script has errors then listing shows where they are.

Hi all
Thanks for the feedback

Script has read/write/test permissions

Works fine from the command line:

[admin@MikroTik] /system script> run Unotelly
status: finished

[admin@MikroTik] /system script> run Unotelly
[admin@MikroTik] /system script> run Unotelly
[admin@Mikrotik] /log print
06:07:09 system,info,account user admin logged in from 10.0.0.2 via ssh
06:07:37 script,info Unotelly: Current IP 105.233.57.110 is not equal to previous IP , update needed
06:07:37 script,info Unotelly: Sending update
06:07:37 info fetch: file “update_by_hash_api?user_hash=xxx” downloaded
06:07:38 script,info Unotelly: Updated with IP 105.233.57.110
06:08:49 wireless,info C0:9F:42:BB:7B:24@wlan1: connected
06:09:19 dhcp,info default assigned 10.0.0.196 to 00:24:8D:2A:9B:31
06:09:39 dhcp,info default deassigned 10.0.0.196 from 00:24:8D:2A:9B:31


Which is as expected. I’ll look at the curly brace thing later though.