Community discussions

MikroTik App
用户头像
hilton
Long time Member
Long time Member
Topic Author
Posts: 634
加入: Thu Sep 07, 2006 5:12 pm
Location:Jozi (aka Johannesburg), South Africa

Trying to use the same variable name in multiple scripts

Tue Aug 16, 2011 3:40 pm

Hi guys

I need to run the same script to resolve a number of host names in order for netwatch to work.

Am I correct in saying that I need to use local variables in order for there not to be some sort of clash?

Here's my script.
Code:Select all
{ :local "netwatch-host-name" "nippy" :local "netwatch-dns-name" "nippy.dyndns.net" :local "new-dns-ip" [:resolve $"netwatch-dns-name"] :local "current-dns-ip" [/tool netwatch find comment=$"netwatch-dns-name" host] :if ($"current-dns-ip" != $"new-dns-ip") do={ /tool netwatch set [find comment=$"netwatch-host-name"] host=$"new-dns-ip"} }
and then script number two with a new variable name
Code:Select all
{ :local "netwatch-host-name" "nippy1" :local "netwatch-dns-name" "nippy1.dyndns.net" :local "new-dns-ip" [:resolve $"netwatch-dns-name"] :local "current-dns-ip" [/tool netwatch find comment=$"netwatch-dns-name" host] :if ($"current-dns-ip" != $"new-dns-ip") do={ /tool netwatch set [find comment=$"netwatch-host-name"] host=$"new-dns-ip"} }
I'm worried that the variable
Code:Select all
"netwatch-host-name"
is the same for every script.

So essentially I need to resolve the 'host' name in netwatch every 30 seconds for a large number of dynamic DNS names.

Should I combine these in one big script or add them together?
Top
tjc
Member Candidate
Member Candidate
Posts: 276
加入: Sun Jul 10, 2011 3:08 am

Re: Trying to use the same variable name in multiple scripts

Wed Aug 17, 2011 4:55 am

Sound like a job for a loop...
Top
用户头像
hilton
Long time Member
Long time Member
Topic Author
Posts: 634
加入: Thu Sep 07, 2006 5:12 pm
Location:Jozi (aka Johannesburg), South Africa

Re: Trying to use the same variable name in multiple scripts

Wed Aug 17, 2011 9:56 am

Something is certainly in a loop because when I set the scheduler to run those scripts every thirty seconds, the netwatch resets so I get the email notification that my site is up every thirty seconds.

Could this be because I've used a local variable and it's value expires after the script has run? Would using a global variable be better?

Can you see a loop anywhere?
Top
用户头像
janisk
MikroTik Support
MikroTik Support
Posts: 6263
加入: Tue Feb 14, 2006 9:46 am
Location:Riga, Latvia

Re: Trying to use the same variable name in multiple scripts

结婚2011年8月17日12:55点

local variables are just that, locally available at the time of script execution. If you want to preserve value you have to go global route and then this variable will be available for the time when router is up and user scripts running. It is similar to scope in c/c++/java

{
:local var
:set var lala
}

var is not accessible any more when script has ended, no matter what

{
:global var
:set var lala
}

var is available. to access it, do the following:
:global var
:put var
Top

Who is online

Users browsing this forum:pothiand 8 guests