Community discussions

MikroTik App
 
User avatar
quotengrote
newbie
Topic Author
Posts: 33
Joined: Sun May 16, 2021 1:20 pm

Variables - why does it not work?

Sat Jan 15, 2022 12:06 am

Hi,

i have that simple Script for netwatch:
:log error "wireguard-tunnel down: wireguard_s2s_ag";
/interface/wireguard/disable wireguard_s2s_ag;
:delay 4000ms;
/interface/wireguard/enable wireguard_s2s_ag;
:log info "Restart wireguard-tunnel: wireguard_s2s_ag";
The first one works, the second not.
# set variables
:local wg_interface wireguard_s2s_ag;

:log error "wireguard-tunnel down: $wg_interface";
/interface/wireguard/disable $wg_interface;
:delay 4000ms;
/interface/wireguard/enable $wg_interface;
:log info "Restart wireguard-tunnel: $wg_interface";
Does anyone has an idea?

Wishes
mg
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3334
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Variables - why does it not work?  [SOLVED]

Sat Jan 15, 2022 12:12 am

Remove the underscore in the variable name.
Use
:local wginterface wireguard_s2s_ag
And
1. You can remove ; at end of all line. Only needed while there are multiple command on same line.
2. :delay 4000ms is the same as :delay 4s
 
Sob
Forum Guru
Forum Guru
Posts: 9188
Joined: Mon Apr 20, 2009 9:11 pm

Re: Variables - why does it not work?

Sat Jan 15, 2022 12:25 am

It's also documented:
Valid characters in variable names are letters and digits. If variable name contains any other character, then variable name should be put in double quotes.
But it would be so much better, it RouterOS scripting had some useful error reporting. Just imagine how it could be, even simple "syntax error at line 1, column 9" would be so beautiful. Maybe one day...
 
User avatar
quotengrote
newbie
Topic Author
Posts: 33
Joined: Sun May 16, 2021 1:20 pm

Re: Variables - why does it not work?

Sat Jan 15, 2022 12:32 am

It worked, thank you very much.
But it would be so much better, it RouterOS scripting had some useful error reporting. Just imagine how it could be, even simple "syntax error at line 1, column 9" would be so beautiful. Maybe one day...
That would be great...

Wishes
mg

PS:

For the docs:
# set variables
:local wginterface wireguard_s2s_ag
# Valid characters in variable names are letters and digits. If variable name contains any other character, then variable name should be put in double quotes.

:log error "wireguard-tunnel down: $wginterface"
/interface/wireguard/disable $wginterface
:delay 4s
/interface/wireguard/enable $wginterface
:log info "Restart wireguard-tunnel: $wginterface"
 
msatter
Forum Guru
Forum Guru
Posts: 2937
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Variables - why does it not work?

Sat Jan 15, 2022 12:58 am

It does....

If you past, notice the {}, this into Terminal it will show you in red the locations where there are errors in the syntax.
{
# set variables
:local wg_interface wireguard_s2s_ag;

:log error "wireguard-tunnel down: $wg_interface";
/interface/wireguard/disable $wg_interface;
:delay 4000ms;
/interface/wireguard/enable $wg_interface;
:log info "Restart wireguard-tunnel: $wg_interface";
}
Showing:
terminal_errors.JPG
...and notice that the log lines do not produce an error because there the underscore is between " ", as documented.
You do not have the required permissions to view the files attached to this post.
Last edited by msatter on Sat Jan 15, 2022 1:02 am, edited 1 time in total.
 
User avatar
quotengrote
newbie
Topic Author
Posts: 33
Joined: Sun May 16, 2021 1:20 pm

Re: Variables - why does it not work?

Sat Jan 15, 2022 12:59 am

Did not know that, thx.
 
Sob
Forum Guru
Forum Guru
Posts: 9188
Joined: Mon Apr 20, 2009 9:11 pm

Re: Variables - why does it not work?

Sat Jan 15, 2022 1:37 am

But that's not even remotely user friendly. If I need to write some longer script, I can't be copying and pasting the whole thing in terminal over and over again, that's terrible. Better than nothing, maybe, but not by much.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1090
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Variables - why does it not work?

Sat Jan 15, 2022 12:29 pm

Well, you can edit the scripts in CLI...
/system script edit Script-Name source
That way you have syntax highlighting all the time.
 
Sob
Forum Guru
Forum Guru
Posts: 9188
Joined: Mon Apr 20, 2009 9:11 pm

Re: Variables - why does it not work?

Sat Jan 15, 2022 6:55 pm

Thanks, I didn't know about that. Quick test says that it's getting closer to something good. For now I dare to say usable. There are some flaws, most obvious is that it saves with Unix line endings, which messes up display in WinBox, where it becomes just one long line. Another problem is with clipboard shortcuts, where in order to have working copy (Ctrl+Ins), I have to use ssh, it doesn't work in Terminal in WinBox. But on the upside, it looks like it will fix troubles with "silent death".

Who is online

Users browsing this forum: No registered users and 7 guests