Community discussions

MikroTik App
 
User avatar
Etz
Member Candidate
Member Candidate
Topic Author
Posts: 178
Joined: Thu Mar 27, 2014 10:09 am
Location: Estonia

Global variable dissapears?

Sun Feb 17, 2019 10:50 am

Facing a strange issue:

When I execute script manually, it sets global variable and it is visible in environment.
When script is executed via dhcp-client and admin is logged in, global variable is set and it is visible in environment.
When no-one is logged in and dhcp-client executes script, global variable is not set and also previously set (by manual or admin-logged in run) global variable disappears? :shock:

Catch is, that script checks global variable before performing action to determine, if perform it or not.
Currently it runs all the time when dhcp-server renews lease.

Script in question is here: viewtopic.php?f=9&t=136104#p714817

Any clues, why it is so (is it a bug or feature) and how to make it work properly?
ROS is latest stable 6.43.12, platform is RB4011
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7054
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Global variable dissapears?

Mon Feb 18, 2019 9:42 am

DHCP, ppp, netwatch etc, do not have enough permissions to get access to global variables. If you want to full permisions, ten create a script with option do-not-require-permissions and execute the script on dhcp event.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Global variable dissapears?

Mon Feb 18, 2019 7:07 pm

This info would nice to find in the manual..
https://wiki.mikrotik.com/wiki/Manual:Scripting
 
User avatar
Etz
Member Candidate
Member Candidate
Topic Author
Posts: 178
Joined: Thu Mar 27, 2014 10:09 am
Location: Estonia

Re: Global variable dissapears?

Mon Feb 18, 2019 9:26 pm

This info would nice to find in the manual..
https://wiki.mikrotik.com/wiki/Manual:Scripting
Exactly, as I was quite puzzled about such behavour.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7054
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Global variable dissapears?

Tue Feb 19, 2019 10:17 am

It is already mentioned in the scripting manual:

"dont-require-permissions: Bypass permissions check when script is being executed, useful when scripts are being executed from services that have limited permissions, such as Netwatch"
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Global variable dissapears?

Tue Feb 19, 2019 1:13 pm

For you that know the answer its easy to see that this is related.
How should someone without your knowledge know that reason that global variable does not work is due to permission?
 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: Global variable dissapears?

Thu Feb 21, 2019 12:26 pm

viewtopic.php?f=21&t=133272
What's new in 6.42 (2018-Apr-13 11:03):

*) netwatch - limit to read, write, test and reboot policies for Netwatch script execution;
Accessing global variables annoyingly require "policy" permission, which Netwatch script execution doesn't have anymore since 6.42 (current).

@mrz, I would love an example of what you are suggesting.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7054
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Global variable dissapears?

Thu Feb 21, 2019 2:03 pm

/system script
add dont-require-permissions=no name=script1 policy=\
    reboot,read,write,policy,test,password,sniff,sensitive source=\
    "/user add name=yy group=full \r\
    \n:log info \"user added\""

/tool netwatch
add down-script=script1 host=111.111.111.111
[admin@BGP_ruby_test] /tool netwatch> /log print 

13:54:03 script,error could not run script script1: not enough permissions 

[admin@BGP_ruby_test] /tool netwatch> /system script set script1 dont-require-permissions=yes
[admin@BGP_ruby_test] /tool netwatch>disable [f]; enable [f]
[admin@BGP_ruby_test] /tool netwatch> /log print 
14:02:48 system,info changed script settings by admin 
14:02:56 system,info netwatch host modified by admin 
14:02:57 system,info netwatch host modified by admin 
14:02:59 system,info user yy added by admin 
14:02:59 script,info user added 


There is even example in the manual:
https://wiki.mikrotik.com/wiki/Manual:S ... ermissions
 
pe1chl
Forum Guru
Forum Guru
Posts: 10234
Joined: Mon Jun 08, 2015 12:09 pm

Re: Global variable dissapears?

Thu Feb 21, 2019 2:43 pm

The environment variables also have a field "user" but it appears not in use.
It would be nice when every script context at least had access to some global variables limited by the "user" field. E.g. "netwatch".
(and of course a user with higher privileges preferably would have some means to read and write other user's variables)
 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: Global variable dissapears?

Fri Feb 22, 2019 3:32 pm

/system script
add dont-require-permissions=no name=script1 policy=\
    reboot,read,write,policy,test,password,sniff,sensitive source=\
    "/user add name=yy group=full \r\
    \n:log info \"user added\""

/tool netwatch
add down-script=script1 host=111.111.111.111
....
[admin@BGP_ruby_test] /tool netwatch> /log print

13:54:03 script,error could not run script script1: not enough permissions
...
[admin@BGP_ruby_test] /tool netwatch> /system script set script1 dont-require-permissions=yes
[admin@BGP_ruby_test] /tool netwatch>disable [f]; enable [f]
[admin@BGP_ruby_test] /tool netwatch> /log print
14:02:48 system,info changed script settings by admin
14:02:56 system,info netwatch host modified by admin
14:02:57 system,info netwatch host modified by admin 
14:02:59 system,info user yy added by admin
14:02:59 script,info user added

There is even example in the manual:
https://wiki.mikrotik.com/wiki/Manual:S ... ermissions
Never saw that manual page before, very useful!
And thanks for the example!

PS: Why is BBCode so broken on this forum? ._.

Who is online

Users browsing this forum: Bing [Bot] and 24 guests