Community discussions

MikroTik App
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 720
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Persistent Environment Variables

Wed Dec 09, 2020 6:47 pm

other than to load them with a script every reboot, is there any way to get an environment variable to persist between multiple reboots?
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Persistent Environment Variables

Thu Dec 10, 2020 1:03 am

No, this was discussed in the 7.1beta3 thread a few days ago.
 
liuyao
just joined
Posts: 9
Joined: Wed Sep 04, 2019 9:14 am
Location: China

Re: Persistent Environment Variables

Sat Jan 30, 2021 11:09 am

by baidu translate :
I have a method to define a schedule to read environment variables to the array every minute. In the definition of the second boot from the start of the schedule. Use the acquired array to write to the second schedule at any time. Maybe the idea is to make it by yourself

There is only one command to demonstrate :
:put [/system script environment print as-value ]
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3343
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Persistent Environment Variables

Sat Jan 30, 2021 1:12 pm

OP did not want a script or using scheduling.

I agree that there should be a way to set persistent variables that survives reboot.
You can add a fw rule it stays trough a reboot, so should a persistent variable do as well.
Last edited by Jotne on Mon Feb 08, 2021 3:48 pm, edited 1 time in total.
 
millenium7
Long time Member
Long time Member
Posts: 579
Joined: Wed Mar 16, 2016 6:12 am

Re: Persistent Environment Variables

Mon Feb 08, 2021 6:17 am

For the moment you can kinda-sorta get away with it by storing variables as comments. The layer7-protocol area is not a bad one because it is practically unused these days so it doesn't clutter up the interface if you put a bunch of variables in there
Comments will not store arrays correctly, or rather the 'toarray' command to convert a string back to an array doesn't work because arrays store with semi-colon separates. Whereas the 'toarray' command uses commas to separate them (why mikrotik why?)

But this method works just fine for simpler things. I use it for incrementing a counter in a few scripts. One of them being a template that runs at a later time, where say a router is told to change some config and then reboot. Well the first command is to increment an integer in the comment of that scheduled task, it's first read and if the counter is higher than say 1 then something has gone wrong, commands havn't executed properly, script has failed, it failed to self-delete (otherwise its just going to reboot every single day at 3am forever) etc so it'll then send an email to support desk as well as a remote syslog message to alert that somethings not right and requires manual intervention
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables  [SOLVED]

Sat Oct 02, 2021 1:30 pm

search tag # rextended save and restore global variables on reboot

This is for save and restore variables with keeping the right data type on restore:

Scheduled script do that every x minutes, on purpose store only variables, but do not store codes or functions
because till now I do not have find one way to do that.
code = from ":parse" function, like :global c [:parse "/interface print"]
function = created like :global f do={ /interface print }, but is reported as array from :typeof

Only simple arrays are exported, do not work for array of array and more complex data structure.

/system script environment
:foreach item in=[find] do={
    :local vname  [get $item name]
    :local vvalue [get $item value]
    :if ($vvalue~"^\\*") do={:set vvalue "ID$vvalue"}
    :if ($vvalue~"^(\\(code\\)|;\?\\(eva\?l )") do={:set vvalue "(code)"}
    /ip firewall layer7
    remove [find where name=$vname]
    add name=$vname comment="$vvalue"
    :delay 10ms
    :execute "/ip firewall layer7 set [find where name=$vname] regexp=[:typeof \$$vname]"
    :if ($vvalue="(code)") do={:delay 10ms ; set [find where name=$vname] regexp="code"}
}
Store variable name... on name, variable type on regexp, variable contents on comment.


On reboot this script is scheduled at startup, this restore also variables that do not contain readable data, like binary data, lower numbers and special characters:
/ip firewall layer7
:foreach item in=[find where regexp~"^(array|bool|code|id|ip|ip-prefix|ip6|ip6-prefix|lookup|nil|nothing|num|str|time)\$"] do={
    :local vname  [get $item name]
    :local vvalue [get $item comment]
    :local vtype  [get $item regexp]
    /system script environment
    remove [find where name=$vname]
    :if ($vtype~"^(array|ip|ip6|num|str|time)\$") do={
        :execute ":global $vname [:to$vtype [/ip firewall layer7 get [find where name=$vname] comment]]"
    } else={
        :if ($vtype~"^(bool|id|ip-prefix|ip6-prefix|lookup|nil|nothing)\$") do={
            :if ($vtype="bool")         do={:execute ":global $vname [:tobool $vvalue]"}
            :if ($vtype="id")           do={:execute ":global $vname [:toid $[:pick $vvalue [:find $vvalue "*" -1] [:len $vvalue]]]"}
            :if (($vtype="ip-prefix") or \
                 ($vtype="ip6-prefix")) do={:execute ":global $vname [[:parse \":return $vvalue\"]]"}
            :if ($vtype="lookup")       do={:execute ":global $vname \"\$$vname\""}
            :if ($vtype="nil")          do={:execute ":global $vname"}
            :if ($vtype="nothing")      do={:execute ":global $vname [:nothing]"}
        } else={
            # vtype="code"
            :log error "Unknow variable >$vname< of type >$vtype<"
            :execute ":global $vname [/ip firewall layer7 get [find where name=$vname] comment]"
        }
    }
    :delay 10ms
}

True Level 7 rules for Firewall are not "touched" unless they regexp is exactly a single word as the names of reserved variable types.


Someone know the full list of variable types???

standard:
array
bool
id
ip
ip6
num
str
time

broken, but restored with an hack:
ip-prefix
ip6-prefix

not recoverable:
code
function (reported as array)

"undefined" types:
nil
nothing
lookup ($0 on functions)

EDIT: added fix for new discovered "op" on RouterOS, thanks to @tabraham report
Last edited by rextended on Tue Aug 29, 2023 12:30 pm, edited 18 times in total.
 
User avatar
Larsa
Forum Guru
Forum Guru
Posts: 1611
Joined: Sat Aug 29, 2015 7:40 pm
Location: The North Pole, Santa's Workshop

Re: Persistent Environment Variables

Sat Oct 02, 2021 8:39 pm

Any word regarding persistent variables in v7 yet?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Sun Oct 03, 2021 3:27 am

Sorry, but I think this is out of interest from MikroTik until the release of RouterOS 7.7.7 (stable)
 
User avatar
Larsa
Forum Guru
Forum Guru
Posts: 1611
Joined: Sat Aug 29, 2015 7:40 pm
Location: The North Pole, Santa's Workshop

Re: Persistent Environment Variables

Sun Oct 03, 2021 9:38 am

Rumors say 7.9 that also will include the ability to catch well documented error numbers and messages.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Mon May 09, 2022 1:11 pm

updated for better support ID, code and function variables,
tested on both v6 and v7
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Persistent Environment Variables

Tue May 10, 2022 7:23 pm

Your script works very well friend REX, the only thing is that how could it be removed from the LOG so that it does not register every time the variables are saved in the LAYER 7





EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Tue May 10, 2022 8:22 pm

you can't without disabling all logs.
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Persistent Environment Variables

Tue May 10, 2022 10:54 pm

ok.







EL DONCITO.
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Mon May 16, 2022 6:26 pm

Very useful this script, thanks!

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Mon May 16, 2022 6:53 pm

Very thanks!

See also on my signature for other scripts ;)
I do not have catalogued all script I have done... one day...
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Tue May 31, 2022 10:46 am

Thanks to the author for the very beautifully written scripts for saving and restoring global variables.

There are comments:
In the save script, you need to remove the save "(function)", since it will not be possible to restore the function code.
Only variables should be saved and restored.

When restoring, restoring boolean variables via :execute does not work for me,
all boolean variables are restored as nil (have no values).

I had to insert such a check and restore via :parse
 :if ($vtype = "bool") do={
[[:parse ":global $vname [:to$vtype $[/ip firewall layer7 get [find where name=$vname] regexp]]"]]}
In this form, everything works.

You also need to remove the line from the recovery script
 :if ($vvalue = "(function)") do={:set vtype "function" }
since the functions are not saved and they do not need to be restored, they will have to be redefined again.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Tue May 31, 2022 3:22 pm

When restoring, restoring boolean variables via :execute does not work for me,
all boolean variables are restored as nil (have no values).

I had to insert such a check and restore via :parse
 :if ($vtype = "bool") do={
[[:parse ":global $vname [:to$vtype $[/ip firewall layer7 get [find where name=$vname] regexp]]"]]}

Yes, a bug, but simply solved with
:if ($vtype="bool") do={:execute ":global $vname [:tobool $vvalue]"}


Thanks for the info, I do some restyling and also fix boolean.

About "(function)" (and CODE!)
it was put there for some reasons:
1) manage all the types as possible,
2) in the future maybe they can be managed directly, and it is already ready
3) is ready for add on script the search for file in (flash/)functions/vname.rsc or (flash/)codes/vname.rsc and load it at startup, if the file exist...
On this way also codes and functions are restored...
But is not intended to store inside the file the code "(eval /interface print)" because also if restored, can be restored only as string,
actually there is no way to convert directly "(eval /interface print)" to code or function.
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Tue May 31, 2022 4:37 pm

It's all clear. But, if you insert the saving of a function variable as, for example, (function), then during the subsequent restoration, the code of active functions in the environment will be corrupted and they will need to be redefined. If you do not save the functions, then you do not need to restore them, then the code of the active environment functions will be saved and they will be operational after restoring global variables of other types.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Tue May 31, 2022 5:38 pm

You forget something: is for restore variables on reboot, not at any arbitrary time...
On reboot no previous variables or functions or codes exists...

And see point 3) on previous post... 8)
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Wed Jun 01, 2022 10:56 am

I have not forgotten, your script is also convenient for saving and restoring variables not only during reboot, but also at any time!
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Wed Jun 01, 2022 10:59 am

I made one function out of it that allows, depending on the value of the passed argument (save, restore, erase and print), to save, restore variables, as well as delete them from the Layer7 storage or print a list of storage variables to the terminal and log
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Wed Jun 01, 2022 11:02 am

I'll run around a little and put it here
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Wed Jun 01, 2022 12:29 pm

Here's what it looks like:
#
# The function of saving/restoring global environment variables v 01/06/2022
# $1 parameter can take values:
# "save" - saving global variables in /ip firewall layer7
# "recover" - restoring global variables from layer7
# "erase" - cleaning layer7 only ! from global variables
# "print" - printing to the log and terminal of the list of variables located at the time of printing in Layer7
#

:global FuncGlobal do={
:if ([:typeof $0]="lookup") do={
:local count 0
:if (($1="save") or ($1="recover") or ($1="erase") or ($1="print")) do={
:if ($1="save") do={
/system script environment
:foreach item in=[find] do={
    :local vname [get $item name]
    :local vvalue [get $item value]
    /ip firewall layer7
    remove [find where name=$vname]
    :if (([:typeof [:find [:tostr $vvalue] "(eval " -1]] = "nil") and ([:typeof [:find [:tostr $vvalue] "(evl " -1]] = "nil")) do={
        :if ([:find $vvalue "*" -1] = 0) do={:set vvalue "ID$vvalue"}
        add name=$vname regex="$vvalue"
        :set count ($count+1)
          } 
        :delay 10ms
        :execute "/ip firewall layer7 set [find where name=$vname] comment=[:typeof \$$vname]"
}
:log warning ("Function $0 has saved $count variables")
:return $count}

:if ($1="recover") do={
/ip firewall layer7
:foreach item in=[find where comment~"^(array|bool|id|ip|ip-prefix|ip6|ip6-prefix|num|str|time|code|nothing|nil)\$"] do={
    :set count ($count+1)
    :local vname [get $item name]
    :local vvalue [get $item regexp]
    :local vtype [get $item comment]
#    :if ($vvalue = "(function)") do={:set vtype "function" }
    /system script environment
    remove [find where name=$vname]
    :if ($vtype~"^(array|bool|id|ip|ip6|num|str|time)\$") do={
        :if ($vtype = "id") do={
            :set vvalue [:pick $vvalue [:find $vvalue "*" -1] [:len $vvalue]]
            :execute ":global $vname [:to$vtype $vvalue]"}
        :if ($vtype = "bool") do={
           [[:parse ":global $vname [:to$vtype $[/ip firewall layer7 get [find where name=$vname] regexp]]"]]
# :if ($vtype="bool") do={:execute ":global $vname [:tobool $vvalue]"}
        } else={
            :execute ":global $vname [:to$vtype [/ip firewall layer7 get [find where name=$vname] regexp]]"
        }
    } else={
        :if ($vtype~"^(ip-prefix|ip6-prefix|nothing|nil)\$") do={
            :if ($vtype~"^(ip-prefix|ip6-prefix)\$") do={:execute ":global $vname [[:parse \":return $vvalue\"]]"}
            :if ($vtype="nothing") do={:execute ":global $vname [:nothing]"}
            :if ($vtype="nil") do={:execute ":global $vname"}
        } else={
            :log error "Unknow variable type >$vtype<"
            :execute ":global $vname [/ip firewall layer7 get [find where name=$vname] regexp]"
        }
    }
    :delay 10ms;
}
:log warning ("Function $0 restored $count variables")
:return $count}

:if ($1="erase") do={
/ip firewall layer7
:foreach item in=[find where comment~"^(array|bool|id|ip|ip-prefix|ip6|ip6-prefix|num|str|time|code|nothing|nil)\$"] do={remove $item; :set count ($count+1)
}
:log warning ("The repository has been cleared of $count global variables")
:return $count} 

:if ($1="print") do={
/ip firewall layer7
:foreach item in=[find where comment~"^(array|bool|id|ip|ip-prefix|ip6|ip6-prefix|num|str|time|code|nothing|nil)\$"] do={
    :set count ($count+1)
    :local vname [get $item name];
    :local vvalue [get $item regexp];
    :local vtype [get $item comment];
 :put ("$vname "."$vtype "."$vvalue");
 :log info ("$vname "."[ $vtype ] "."$vvalue");
}
:log warning ("In Layer7 repository has $count global variables")
:put ("In Layer7 repository has $count global variables")
:return $count}

} else={
:if ([:typeof $1]="nothing") do={:log error ("The function $0 parameter is not set"); :return ("Error $0")}}        
 :log error ("The function $0 parameter is not valid"); :return ("Error $0");
 }
}

# usage examples:
 
# :log info [$FuncGlobal save]
# :log info [$FuncGlobal recover]
# :log info [$FuncGlobal erase]
# :log info [$FuncGlobal print]

# examples of erroneous usage:

# :log info [$FuncGlobal]
# :log info [$FuncGlobal anovertext]
The function can be called to save variables with the command [$FuncGlobal save] with the desired frequency. Recovery from layer7 is possible at any time, including at the start of the router. To clear leyer7 of global variables, use [$FuncGlobal erase]. It is planned to finalize with the introduction of restoring only part of the variables (for example, with a certain prefix of names)
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Wed Jun 01, 2022 2:54 pm

Very interesting, thanks!

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed Jun 01, 2022 10:53 pm

@Sertik,
Thanks, as usual, for pointing out the original author of the script... 🤨
Really kind...
 
eldoncito2019
Member
Member
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Persistent Environment Variables

Mon Aug 08, 2022 9:17 pm

and if i want to save an ip variable in ip firewall address list and then restore it back on reboot?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Mon Aug 08, 2022 9:24 pm

Don't start with the absurd questions, like the other topic. ( viewtopic.php?t=187812 )
If you save something statically in the firewall's ip address list, it is automatically restored on reboot, without you needing to do anything.

Open your topic, if you don't understand, without going offtopic here.
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Tue May 23, 2023 5:55 pm

Hi rextended, when I run your first script to save the global variables I get an error:

ROS Version 7.9.1
[admin@MikroTik] > sys scr run save-global-variables
failure: bad regexp: unmatched ()

ROS Version 7.10beta5
[admin@MikroTik] > sys scr run save-global-variables
failure: bad regexp: too many ()

Could you check it? thanks.

BR.
Last edited by diamuxin on Wed May 24, 2023 10:51 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 10:38 am

Since this method use RegEx inside layer 7 to save variables, check if on some variables are present too many ( ) and let me know if the problem is one variable or one function/code...

add
:put $vname
after
:local vname [get $item name]
for print what variable is the problem and check the variable/function/code content...

...actually perhaps it is better that I change the point where to save the variables, otherwise if there are too many parentheses inside a textual variable it gives an error as if it were a function.


EDIT: switched regex/comment functionality, now (except for functions, codes and nested arrays) can work.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 11:08 am

...
You try the "new version" if do same error?
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Wed May 24, 2023 11:57 am

You try the "new version" if do same error?
Hi Rex,

Sorry for the delay, I just tested both scripts and they are now working correctly, both in v7.9.1 and v10beta5.

I am also happy because I see that it is now possible to save/restore the content of a function. Congratulations!

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 12:05 pm

[…] it is now possible to save/restore the content of a function. Congratulations!
EEEEEEEHHHHHHHHHHHHH???????????
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Wed May 24, 2023 12:08 pm

[…] it is now possible to save/restore the content of a function. Congratulations!
EEEEEEEHHHHHHHHHHHHH???????????
I am going to capture screenshots, just a second

--------

Ready:

a) Backup to Layer7

Image

b) Restore to Environment

Image

Am I right?
Last edited by diamuxin on Wed May 24, 2023 12:17 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 12:09 pm

Check twice.... after reboot....

If the function is
:global f do={ /interface print }

It's normal this:
:put [$f]
(function)

Is just a text "(function)" or another script set again the function correctly...
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Wed May 24, 2023 12:20 pm

Check twice.... after reboot....

If the function is
:global f do={ /interface print }

It's normal this:
:put [$f]
(function)

Is just a text "(function)" or another script set again the function correctly...
look at the screenshots

..
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 12:33 pm

Yes, but the function work?

Is easy copy & paste the "eval / evl" code, but is restored just as string...

on the code is present:
:if ($vvalue~"\\(eval|evl) ") do={:set vvalue "(function)"}

is missing one (another) ( before eval, now the script is fixed
 
User avatar
diamuxin
Member
Member
Posts: 340
Joined: Thu Sep 09, 2021 5:46 pm

Re: Persistent Environment Variables

Wed May 24, 2023 1:08 pm

Yes, but the function work?

Is easy copy & paste the "eval / evl" code, but is restored just as string...

on the code is present:
:if ($vvalue~"\\(eval|evl) ") do={:set vvalue "(function)"}

is missing one (another) ( before eval, now the script is fixed
You are right, some functions are not restored properly. With the last modification you have made now save the functions as "(function)" in the comments.

Everything is ok, thanks.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed May 24, 2023 1:17 pm

maybe in the future they will allow to save the functions, in the end it is only text that is stored with a 1 bit instead of 0....
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Mon Jun 05, 2023 3:01 pm

If the Mikrotik developers allow saving the function code in Layer7 and restoring it from there ... this can lead to both good (for example, creating copyright protection) and dangerous consequences (for example, creating virus scripts)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Mon Jun 05, 2023 3:09 pm

If the Mikrotik developers allow saving the function code in Layer7 and restoring it from there ... this can lead to both good (for example, creating copyright protection) and dangerous consequences (for example, creating virus scripts)
Completely nonsense: if a "virus" has access to the routerboard, it doesn't give a damn about "reading" functions from Layer7,
it simply puts the function it likes into memory as it already does now...

And speaking of copyrighted materials, I would never trust to insert any script of which I don't see the unencrypted source,
there is already trust in what MikroTik writes in its sources, without letting us see them...
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Persistent Environment Variables

Mon Jun 05, 2023 3:23 pm

The future will show nonsense or not nonsense.
 
flydvorkin
just joined
Posts: 16
Joined: Mon Mar 11, 2019 12:59 pm

Re: Persistent Environment Variables

Sat Jun 17, 2023 1:46 am

I found another lost data type:
:put [:typeof (>[])]
Return:
op

this data type behaves like code
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Sat Jun 17, 2023 2:22 am

I found another lost data type:
:put [:typeof (>[])]
Return:
op

this data type behaves like code
????????????????????????????????????????????????????????????

Thanks........
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Persistent Environment Variables

Sat Jun 17, 2023 2:44 am

I found another lost data type:

Fascinating...
:global optype (>[:global z "blah"])                   
$optype                             
:put $z
# "blah"

Even can be a function with params using :do...
:global optype (>[:do {:put "$1"}])

:put [:typeof $optype]             
# op

$optype "hmm"                      
# hmm

:put $optype
# (evl (evl /docommand=;(evl (evl /putmessage=$1))))

Or with (>{}) it's an empty array...
:global shortThanToArray (>{})
:put "$shortThanToArray $[:typeof $shortThanToArray] $[:len $shortThanToArray]" 
 # array 0

And according /console/inspect, it valid syntax like "(>"... e.g.
/console/inspect input="(" request=completion
Columns: TYPE, COMPLETION, STYLE, OFFSET, PREFERENCE, SHOW
TYPE COMPLETION STYLE OFFSET PREFERENCE SHOW
completion none 2 80 no
completion ( syntax-meta 2 75 no
completion $ syntax-meta 2 75 no
completion [ syntax-meta 2 75 no
completion { syntax-meta 2 75 no
completion " syntax-meta 2 75 no
completion ! syntax-meta 2 75 no
completion - syntax-meta 2 75 no
completion ~ syntax-meta 2 75 no
completion > syntax-meta 2 75 no
completion syntax-meta 2 75 no
completion <value> none 2 -1 no
 
flydvorkin
just joined
Posts: 16
Joined: Mon Mar 11, 2019 12:59 pm

Re: Persistent Environment Variables

Fri Jun 23, 2023 1:39 am

Also i found that the 2 this variants to print "code":
1 - directly printing by "put":
:put $varWithCodeDataType
or
:put [ :parse "local var" ]
or
:put ( $function->1 )
2 - print global function parsed code value from /system script environment
:put [ /system script environment get [ find name="globalFunction" ] value ]
both this variants printing text of parsed code is not completely.
For comparison use 3-rd method:
/environment print
this printing ALL global variables, but if find the needed text of the code in the printout - will see difference with 1st and 2nd methods:

For example, let source:
local var
then parse them to code-data-type and printing with all methods.

1st and 2nd methods are returned:
(evl /localname=$var)
but 3rd method (store code to global variable and print) are returned:
{(evl [/local{name=$var}])}
It seems 3rd method are more preferred to analyse parsed code.

Unfortunately, the "/environment print" command are not have any arguments. But it possible redirect console output with :execute to file (or directly in variable with ros 7.8+ by "as-string" argument), and extract needed part.
I wrote a usefull function that does all this work automatically
Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Mon Jun 26, 2023 12:59 am

op..."only pointer" ?

Probably is only for point data inside complex array without everytime specify full path...

Experimental code

:global arraytest {"a";"b";"c";{"q";"z"}}
:put $arraytest

:global pointer (>($arraytest->1))
:put $pointer
:put [$pointer]

:set ($arraytest->1) "x" 
:put $arraytest
:put $pointer
:put [$pointer]

:set [$pointer] "W"
:set pointer "J"
:put $arraytest
:put $pointer
:put [$pointer]

:global pointer (>($arraytest->3))
:put $pointer
:put [$pointer]
:put ([$pointer]->1)

:set ([$pointer]->1) "RR"
:put $pointer
:put [$pointer]
:put $arraytest
Last edited by rextended on Tue Aug 29, 2023 12:32 pm, edited 1 time in total.
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Topic Author
Posts: 720
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Persistent Environment Variables

Mon Jul 31, 2023 6:55 pm

I found another lost data type:
:put [:typeof (>[])]
Return:
op

this data type behaves like code
Anyway to use this data type to write a script that uses mac-telnet to log into another device by MAC address and execute a command? :?: Something I've been struggling with since there seems to be no way to pass credentials into the mac-telnet command once you launch it. (and since someone will suggest it, no I cannot SSH into the device, this need exists in situations where there is no IP address defined on a device, it is ONLY accessible via mac-telnet)
 
tabraham
newbie
Posts: 27
Joined: Wed Feb 08, 2017 10:18 pm
Location: Germany
Contact:

Re: Persistent Environment Variables

Tue Aug 29, 2023 11:06 am

Hi Rex. The code seams to break in ROS 7.11 stable. for the store script
:if ($vvalue~"^(\(code\)|;\\(eva\?l )") do={:set $vvalue "(code)"};
The break is mainly in the regex part. could you please double check? Thanks
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Tue Aug 29, 2023 12:18 pm

Hi Rex. The code seams to break in ROS 7.11 stable. for the store script
:if ($vvalue~"^(\(code\)|;\\(eva\?l )") do={:set $vvalue "(code)"};
The break is mainly in the regex part. could you please double check? Thanks

This is not the original line, for example, someone added $ in front of vvalue on :set, and also add ";" at the end, etc....

I took the opportunity to add the control for the export of "op" (\? in front of ; on the RegEx).
 
tabraham
newbie
Posts: 27
Joined: Wed Feb 08, 2017 10:18 pm
Location: Germany
Contact:

Re: Persistent Environment Variables

Wed Aug 30, 2023 10:07 am

I see. Then could you please confirm that the code above is the final version written by you works for arrays too?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Persistent Environment Variables

Wed Aug 30, 2023 10:37 am

Only simple arrays are exported, do not work for array of array and more complex data structure.

Who is online

Users browsing this forum: No registered users and 8 guests