Due to the global variables in RouterOS having bugs (based on the research I’ve done), I am trying to use comments on interfaces as a work-around. I am able to set the comment on interfaces using a script, but I’m not able to retrieve the comment in a script.
For example:
/ppp secrets Iinterfacename comment=“mycomment” sets the comment but how do I retrieve it?
I’ve tried a few things, I imagine it to be something like:
:local var get comment where name=“Iinterfacename”
What are those bugs? I did some scripting and sure it sometimes behaves in a weird way when you made errors
or assumed things worked in the same way as with other scripting languages, but global variables work OK for
me once I had ironed out the misconceptions.
(e.g. I use a global array variable to hold data for a list of MAC addresses and resetting it to an empty list was
afrustrating experience at first)
]For example:
/ppp secrets Iinterfacename comment=“mycomment” sets the comment but how do I retrieve it?
I’ve tried a few things, I imagine it to be something like:
:local var get comment where name=“Iinterfacename”
It is a common method to identify objects, but usually you would set the comment and then use the comment as
a selection criterium later. So you would not “get” it but use something like [find where comment=“xxx”] or
a partial match using ~
I am having many false alerts using Netwatch so I’m trying to build my own script. I want to ping a remote VPN client, then store it’s status somewhere(global variable or comment). Then run the script again in x seconds and if it is still down the script will alert me.
I have tried to store something in a global variable, the first time a script runs
:global var “xxxx”
Then on the next run of the script it could mean back empty
:log info $var
Impossible to say in such a forum dialogue. Scripts depend on details and when there is something wrong (even small syntax errors) it just doesn’t work and is very difficult to find why.
At least you should post your entire script.
Ok, I have given the 3 different ways I have tried to work with Global Variables.
What I am trying to do is to set the variable on the first time the script runs(so expecting an empty in logs from :log info $myVar). The second time the script runs I expect to get the value in the logs, but continue to get nothing(just an empty line in the log).
#GlobalTest#
#log the current value of the global variable
:log info $myVar
#set the value of the global variable
:global myVar "myVarValue";
#GlobalTestMethod2#
#log the current value of the global variable
:log info $myVar
#set the value of the global variable
:global myVar;
:set $myVar value="myVarValue";
#GlobalTestMethod3#
#log the current value of the global variable
:log info $myVar
#set the value of the global variable
:global myVar
:set $myVar false
You need to declare the variable using :global BEFORE your first use!
So you cannot first log it and then declare and set it.
Unfortunately you cannot log a variable when the value is undefined, because it will cause an undefined-value error. You have to
use an on-error handler for cases like that (even when the on-error clause is just empty).
Unfortunately, when you get errors in a background script (like a scheduled script or an on-event script) you never see the error messages.
That is why I wrote that all the details are important and such a code snippet is not enough to debug or discuss it.