How to auto-start a script at interface link up / down ?

How to auto-start a user script at interface link up, and auto-start another user script at interface link down?
Is this possible in RouterOS? If not, then any alternative solution possible, ie. a workaround?
I’m seeking a solution for CRS switch devices with RouterOS in Bridge Mode (I think the procedure could be the same on router devices).

It is not possible in the generic case. In some special cases it is, e.g. for a PPP-type interface.
The workaround is to make a script that is scheduled at some interval, and keep the previous status in a global variable.
You script compares the current status with the previous status in the global variable, acts accordingly, and finally copies the current status to the variable before it exits.

Too bad. I think MT should implement such a functionality. After all it’s already present in the underlying Linux OS,
MT should just write a wrapper on top of it for ROS.
Ok, whatever, I’ll try to solve my problem with the said workaround using the scheduler. Lucky me it’s not that a time-critical task.
Thank you.

Update:
Already encountered my first problem with this approach: how can I check whether a global variable already exists? (forgive me, I’m a newbie in ROS :slight_smile:)
Because it of course wouldn’t be correct if the variable gets initialized in each time interval when the script gets fired… :slight_smile:

Well, if it had been a router interface, you could have set up a netwatch for the interface IP address. But I agree, a generic script trigger on interface state change would be useful.

Why is it not possible to output $gArr in this code (in the “:put $gArr” line) ? :

{
:local f [:tobool [:len $gArr]]
:if ($f = false) do={
:global gArr [:toarray “”]
:set ($gArr->“key1”) “val1”
}

:put $gArr

}

>

gArr gets filled, but it does not output it in this script. Why not?
OTOH doing the same ":put $gArr" in the shell prompt does it right. That's really weirdo, man! :slight_smile:

You can print in /system script environment.

If :global is not found set it with a value:

$shortname contains the variable name used in environment.

:if ([/system script environment find name=$shortName]) do={} else={[:parse "global $shortName 99"];};

This is a script installed under /system/script under a name “test”.
Executing it from inside /system/script with this command:
run test
It executes it, but the said line in the script does not print anything.

Or do you mean something else I maybe have overlooked?

@msatter, I need to do it inside the script, not myself manually in the terminal. Is that not possible?

I gave two answers to two questions.

In environment all globals live. If not there then it is not a global.

Why do you need print while you have put and set?

For your script it is known that tobool is broken since a long time.

Man, by print I really mean this “:put” function or command or how else it is called.
Why not? I’m just playing around & trying to learn this funny language.

The print is a official command like put is. Print can do other stuff then put and they are used to their specific purpose.

Print is print and put is printing using put. Some things are just not straight forward.

The variable is returned as a string and tobool will not convert it. You could just compare to the word true or false. Otherwise you first have to set again depending on the string value.

Is this documented somewhere?
I did not see any such warning in the official scripting web page documentation where I had found it.

In the said script “:put” is used, I just said “printing”. But it’s clear from the context what is meant.

Most of the time but as soon you enter /system script environment and want see the values then you use print. It is a mix and one part of you line you are in printing area and to print the variable outside that, put is used.

If you can’t print by using put try print, is one thing to remind.

Can please anybody else take a look at my code in #5 ? Thx.

What’s new in 6.46beta44 (2019-Sep-19 05:54):

Changes in this release:

*) capsman - fixed channel auto reselection;
*) chr - added support for Azure guest agent;
*) console - fixed “tobool” conversion;

As you noticed, it is not.

It is not what? Not fixed?
Come on, that is the changelog of last year, and it clearly says that it is fixed. And I’m using the 7.0beta5 from 2020.

You are barking up the wrong tree here.

I had this exact same problem a few weeks ago and I worked around it going with returned string value.

I would say there is an error in the script parser regarding “visibility” of scope levels…

I am not a programmer but this is my solution:

{
  :set $f [:len $gArr]
  :if ($f = "true") do={:set $f true} else={:set $f false};
    :if ($f) do={} else={
    :global gArr [:toarray ""]
    :set ($gArr->"key1") "val1"
  }

  :put [:pick $gArr 0]

}

Nothing is being printed but the value is there “key1=val1”.

You initialize the variable inside a block, thus it’s not visible outside.