Community discussions

MikroTik App
 
Heathy65
just joined
Topic Author
Posts: 13
Joined: Wed May 28, 2014 9:54 pm

Using global variable between scripts

Wed Nov 12, 2014 11:49 am

I have this script (which is a slightly modified version of a script on the mikrotik site http://wiki.mikrotik.com/wiki/Dynamic_D ... for_dynDNS, the v5.x version)
# Set needed variables
:local username “<username>"
:local password “<password>"
:local hostname “<hostname>"

:global dyndnsForce
:global previousIP

# print some debug info
#:log info ("UpdateDynDNS: username = $username")
#:log info ("UpdateDynDNS: hostname = $hostname")
#:log info ("UpdateDynDNS: previousIP = $previousIP")

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/usb1/dyndns/dyndns.checkip.html"
:local result [/file get usb1/dyndns/dyndns.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "</body>" -1]
:local currentIP [:pick $result $startLoc $endLoc]
#:log info "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging, but you could end up getting blacklisted by DynDNS!
#:set dyndnsForce true

# Determine if dyndns update is needed
# more dyndns updater request details available at http://www.dyndns.com/developers/specs/syntax.html
:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
    :set dyndnsForce false
    /tool fetch user=$username password=$password mode=http address="members.dyndns.org" src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/usb1/dyndns/dyndns.txt"
    :local result [/file get usb1/dyndns/dyndns.txt contents]
#  :log info ("UpdateDynDNS: Dyndns update needed")
#  :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
    :log info ("UpdateDynDNS: Update needed - previousIP = $previousIP / currentIP = $currentIP [Result: $result]")
    :set previousIP $currentIP
#  :put ("Dyndns Update Result: ".$result)
} else={
#  :log info ("UpdateDynDNS: currentIP = $currentIP - No dyndns update needed")
}
I've got this running every minute via a schedule, so to avoid filling up my logs with repeated entries I've commended out most of the ":log info" statements only leaving one which will be triggered if there's a DynDNS update needed.

This script has the option to force an update by setting the ":set dyndnsForce true" command. Rather than do this in this script (which would force an update every time) I want to have another script, which only runs once a day which changes this global variable to true. Then this main script will do a forced update once a day.

The problem is I can't get this to work, so my understanding of global variables is flawed I guess?

When using global variables across 2 scripts I assume I need to declare them in both scripts with ":global dyndnsForce"

This is what I have in my 2nd script:
:global dyndnsForce
:set dyndnsForce true
Any thoughts appreciated.

FYI I'm running 6.21.1
 
noib
Member Candidate
Member Candidate
Posts: 291
Joined: Fri Jan 25, 2013 6:04 pm
Location: France
Contact:

Re: Using global variable between scripts

Wed Nov 12, 2014 12:54 pm

You can use comments to store data permanently.
/interface ethernet set ether1 comment=$myData
and retrieve them later (even after reboot etc)
:local myData [/interface ethernet get ether1 comment ];
It's a bit dirty but it works :p
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3108
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Using global variable between scripts

Wed Nov 12, 2014 1:08 pm

Do you have "write" policy set for the script and for the shedule ?
 
Heathy65
just joined
Topic Author
Posts: 13
Joined: Wed May 28, 2014 9:54 pm

Re: Using global variable between scripts

Wed Nov 12, 2014 10:19 pm

"noib" thanks for the idea, that's quite clever.

"BartoszP" yes I have the write policy set for the script (I've been running this script manually to test it, not via a schedule).
 
Heathy65
just joined
Topic Author
Posts: 13
Joined: Wed May 28, 2014 9:54 pm

Re: Using global variable between scripts

Wed Nov 12, 2014 11:07 pm

Right I did some experimentation with 3 scripts and a single global variable...
# script testvarJustPrint
:global testvar
:log info ("script= testvarJustPrint : testvar= $testvar")
# script testvarSetFalse
:global testvar
:set testvar false
:log info ("script= testvarSetFalse : testvar= $testvar")
# script testvarSetTrue
:global testvar
:set testvar true
:log info ("script= testvarSetTrue : testvar= $testvar")
Then I ran them in turn and checked the logs.
After a bit of experimentation I've found that all of these policies need to be set in each script for the global variable to be passed between scripts.
  • read
  • write
  • policy
  • test