Community discussions

MikroTik App
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Built in function library

Thu Mar 08, 2018 4:56 pm

We are considering to add commonly used functions as built-in.
What functions would you like to see?
For example, MD5hash, random number generator and so on.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Built in function library

Thu Mar 08, 2018 5:22 pm

From most important to least importing in my opinion:

String replacement. Both by a string needle, and a regex match.

String splitting (into an array of strings). Both by a string needle, and a regex match.

Joining strings in an array into a single string with a specified string between each array member (implicitly convert array values with ":tostr").

Date and time manipulation functions, such as returning the difference between two dates as a "time" value, or adding/subtracting a "time" value to a date (or "num" that is treated as plus seconds), to get a new date at that offset. Also timestamp convertion, i.e. a function to convert a "num" value to a date and time from the unix epoch, and convert a date to a "num" value that is such a timestamp. Being able to output the date and time in a different format would be nice, though it should always default to RouterOS' current format. To that particular end, it would be awesome if there are two new scripting types - date, and datetime, to complement the already existing "time" type, along with type conversion functions. Places within RouterOS that have dates and datetimes should be updated to return typed values instead of strings.

String encoding and decoding functions. Notably, a function to convert a number to a character in a charset (defaulting to ASCII, should support at minimum that and UTF-8), and vice versa. Some other commonly used encoding schemes, such as raw URL encoding/decoding, hex encoding/decoding, hex encoding with a "\" (when generating RouterOS scripts) would also be great.

Array manipulation functions. Such as a function for unique values in an array, getting the key at an index (return "nothing" if there isn't one), sorting an array by key (with an option what to do for keyless members), or by value, in either ascending or descending order, ideally with the option to specify a comparrison function.

In addition to MD5, SHA1, SHA256 and any other OpenSSL supported hashes would be nice, for the purposes of computing hashes to either be posted to a 3rd party or comparing 3rd party checksums. On a related note, encrypting, decrypting and signing data (strings and files) with certificates in the "/certificate" menu (ala PGP) would be awesome.

In addition to a random number between two values, a "random bytes" function (where you just specify the number of bytes you want) would be nice too.
 
User avatar
Joni
Member Candidate
Member Candidate
Posts: 157
Joined: Fri Mar 20, 2015 2:46 pm
Contact:

Re: Built in function library

Fri Mar 09, 2018 8:52 am

If you want to really jumpstart the Mikrotik scripting community then you should probably review the php most common sought after functions.
Also review scripts made for Mikrotik and the most commonly created functions there.
Personally any and all validation functions (ip, dns, email, url, time, date, etc) would be extremely helpfull for anyone trying to implement anything.
(disclaimer, this has nothing to do with php itself but the most adopted language by beginners, cant argue with history)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Built in function library

Mon Mar 12, 2018 5:15 pm

Oh, oh... one more important idea... A function to show the type and value of a variable. When outputting strings or arrays, include lengths, and when outputting strings, surround them with quotes and escape quotes, slashes and unprintable characters inside (so that what's shown could potentially be copy&pasted into a script or parsed). Something analogus to PHP's var_dump(), except that arrays will be shown with both indexes and keys (since RouterOS arrays can have both, whereas PHP can only have either), and perhaps return the string instead of outputting it on screen (allowing users to send it, store it in a file, etc.; not just output it on screen).

This same function, in addition to being useful when debugging scripts, will also enable RouterOS to output reliably parsable data from "print as-value", as opposed to print's other formats, that can all be broken if the user has set a certain value to a free form string field (e.g. comment).
 
User avatar
nz_monkey
Forum Guru
Forum Guru
Posts: 2182
Joined: Mon Jan 14, 2008 1:53 pm
Location: Over the Rainbow
Contact:

Re: Built in function library

Mon Mar 12, 2018 10:56 pm

I was about to post what I thought was important, then realized boen_robot has written everything I was going to :D

So +1 to everything he said..
 
User avatar
Joni
Member Candidate
Member Candidate
Posts: 157
Joined: Fri Mar 20, 2015 2:46 pm
Contact:

Re: Built in function library

Tue Mar 13, 2018 9:48 am

You could also take into consideration the future possibility of executing scheduled remote scripts from The Dude on remote Device.
(ex collect backups from monitored devices)
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 21893
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Built in function library

Tue Mar 13, 2018 1:41 pm

Easy, a function that creates Ports/Services groups, which then can be used in port forwarding and firewall rules etc........
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Tue Mar 13, 2018 2:00 pm

I was about to post what I thought was important, then realized boen_robot has written everything I was going to :D

So +1 to everything he said..
I fully agree with that!
 
ivanfm
newbie
Posts: 48
Joined: Sun May 20, 2012 5:07 pm

Re: Built in function library

Tue Mar 13, 2018 7:01 pm

My suggestions :

ISODateTime - returns date/time in ISO format, great for saving backup files, that can be ordered, currently the ROS date used month as text.

FlashPrefix - receive an filename and return it with /flash/ when the device has an /flash directory to keep saved files

Function to keep only some characters from string (used to create safe filenames)

Replace characters on string , given an string, old character and new character all old characters found in the string are replaced by new character

Modulus of division (better to be an operator instead of function)

function to sort an array, by ascii strings or by numeric values.

hash function returning values in hex.
 
User avatar
omega-00
Forum Guru
Forum Guru
Posts: 1167
Joined: Sat Jun 06, 2009 4:54 am
Location: Australia
Contact:

Re: Built in function library

Wed Mar 14, 2018 2:27 pm

1. The implementation of a switch function as an alternative to if,else.. or at least if,elseif,else.
2. Add back in the LUA support for editing items/utilising datasets above 4k characters long.
3. A random function would be very handy
4. All the things boen said.

A better debugger would also be nice, but now I'm just being picky. :-)
 
thobias
newbie
Posts: 26
Joined: Thu Nov 30, 2017 8:45 pm

Re: Built in function library

Thu Mar 22, 2018 12:01 pm

Date/time comparison (NOW();) and option to get time in different formats.
Json parsing.
LUA or whatever to be able to parse larger files.
 
User avatar
armandfumal
Member Candidate
Member Candidate
Posts: 163
Joined: Wed Apr 25, 2012 5:50 pm
Location: Weiswampach,LUX
Contact:

Re: Built in function library

Thu Apr 12, 2018 8:51 am

url_encode();
 
Chupakabra303
just joined
Posts: 14
Joined: Tue Jun 20, 2017 3:07 pm

Re: Built in function library

Wed Apr 18, 2018 1:55 pm

LUA +1
Internal JSON parser to associative array variable (by type http://www.embest.ru/mikrotik/json-parser-script)
Last edited by Chupakabra303 on Wed Jun 27, 2018 4:50 pm, edited 2 times in total.
 
thantoldo
just joined
Posts: 22
Joined: Tue Apr 10, 2012 10:08 pm

Re: Built in function library

Thu Apr 19, 2018 11:23 am

Can you please raise the fetch variable 65535 character limit (double it or remove it completely), so that the error case "max line length 65535 exceeded!" is resolved?
It happens both with GET and POST.

As routerOS is more and more involved in the big data domain, it is imperative that such limitations are removed.

Also, is there a hardcoded fetch timeout? Can it be exposed as a fetch parameter?
 
shdwmstff
newbie
Posts: 48
Joined: Wed May 30, 2012 12:00 am

Re: Built in function library

Fri Apr 20, 2018 10:57 pm

SNMP-GET
I would like to poll external devices and have based on the results change routes, enable/disable interfaces.
It would be a nice feature.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Sat Apr 21, 2018 1:45 am

SNMP-GET
I would like to poll external devices and have based on the results change routes, enable/disable interfaces.
It would be a nice feature.
I have been thinking about polling SNR/CCQ of radio links and tuning BGP parameters using the results.
First from some external Linux system, when it is workable it would be nice when the router itself could do it.
 
telepro
Frequent Visitor
Frequent Visitor
Posts: 71
Joined: Sun Apr 03, 2011 7:50 pm

Re: Built in function library

Mon May 14, 2018 4:03 am

+1 for SHA1 and SHA256 hash function
better debugging environment
indication of the line number instead of no response when attempting to start a script with an syntax error
output of line number when importing a script encounters a run-time failure
 
User avatar
doneware
Trainer
Trainer
Posts: 647
Joined: Mon Oct 08, 2012 8:39 pm
Location: Hungary

Re: Built in function library

Thu Jul 12, 2018 7:38 pm

JSON encoder
regexp (PCRE) string manipulation
 
Mantrid
just joined
Posts: 2
Joined: Mon Jun 04, 2018 8:53 pm

Re: Built in function library

Fri Jul 27, 2018 3:53 am

function for in RAM memory save\load\parse any big raw data.
"tool fetch", any backup and e.t.c scripts need save data to NAND ... NAND have write limit or small size
Mikrotik need any power functions for manipulate big data in available RAM.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Fri Aug 24, 2018 4:01 pm

Would be nice the see the bitwise operator for IPv6 addresses...
[admin@MikroTik] > :put (192.168.88.10 & 255.255.0.0)
192.168.0.0
[admin@MikroTik] > :put (2003:cf:2f1e:5c00:d250:99ff:fec0:d180 & ffff:ffff:ffff:ff00::)
Script Error: cannot compute bitwise "and" of internal number and internal number
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Fri Aug 24, 2018 4:34 pm

bitwise operator works, whats missing is :toipv6
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Fri Aug 24, 2018 5:25 pm

bitwise operator works, whats missing is :toipv6
Missing from being implemented or missing in my command?
:put ([:toip6 2003:cf:2f1e:5c00:d250:99ff:fec0:d180 ] & [:toip6 ffff:ffff:ffff:ff00::])
... does not give an error, but does not give output neither.
I mailed support and they told me "to be aware of the issue, perhaps it will be fixed in the upcoming versions" (see Ticket#2018061222006824).
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Tue Aug 28, 2018 1:17 pm

bitwise operator works, whats missing is :toipv6
Missing from being implemented or missing in my command?
[admin@MikroTik] > :put ([:toip6 2003:cf:2f1e:5c00:d250:99ff:fec0:d180 ] & [:toip6 ffff:ffff:ffff:ff00::])
... does not give an error, but does not give output neither.
I mailed support and they told me "to be aware of the issue, perhaps it will be fixed in the upcoming versions" (see Ticket#2018061222006824).
mrz, can you give advise?

BTW, this looks borked, no?
:put [:typeof [:toip6 ffff::/64]]
nil
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Tue Aug 28, 2018 2:04 pm

Yes it is not implemented.
 
User avatar
rendezz
just joined
Posts: 16
Joined: Mon Sep 17, 2018 11:07 am

Re: Built in function library

Mon Sep 17, 2018 11:09 am

url_encode();
+1 to this, my implementation is limited and awful
 
User avatar
Usermikrotik
just joined
Posts: 3
Joined: Wed Nov 22, 2017 12:03 pm

Re: Built in function library

Thu Sep 27, 2018 12:10 pm

simulate a runtime command and send it to a running command in a script like in these commands

Image
 
howdey57
Member Candidate
Member Candidate
Posts: 137
Joined: Wed Dec 31, 2014 2:36 pm

Re: Built in function library

Mon Oct 08, 2018 11:23 am

I'd like to have a debug function that helped with basic syntax. When writing code I spend most of my time getting the basics in place. I write my logic then comment most of it out then uncomment line by line to make sure each line is working. With ros code, if it doesn't work, you get nothing to say where the error is

Also,
- log dates in YYYYMMDD HHMMSS format.
- log dates that work around midnight when not in GMT.
- Ability to add items to end of arrays

Charles
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Mon Oct 08, 2018 11:35 am

I'd like to have a debug function that helped with basic syntax. When writing code I spend most of my time getting the basics in place. I write my logic then comment most of it out then uncomment line by line to make sure each line is working. With ros code, if it doesn't work, you get nothing to say where the error is
You may paste the script into Terminal after "{" - that will prevent it from running line by line. Then "}" and Enter to execute.
- Ability to add items to end of arrays
Looks like "+" works just fine:
{
	:local a {1;2;3};
	:set a ($a + 5);
	:foreach v in=$a do={
		:put $v
	};
}
 
howdey57
Member Candidate
Member Candidate
Posts: 137
Joined: Wed Dec 31, 2014 2:36 pm

Re: Built in function library

Mon Oct 08, 2018 11:48 am

Thanks. I'll try those. Are they mentioned in the wiki?

Any thoughts on the dates?
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Mon Oct 08, 2018 11:51 am

Are they mentioned in the wiki?
Not sure :)
Any thoughts on the dates?
I didn't get what's wrong with dates, sorry
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Mon Oct 08, 2018 11:55 am

- Ability to add items to end of arrays
Looks like "+" works just fine:
No, the documentation says using a comma is the correct way:
:set a ($a, 5);
https://wiki.mikrotik.com/wiki/Manual:S ... _Operators
 
howdey57
Member Candidate
Member Candidate
Posts: 137
Joined: Wed Dec 31, 2014 2:36 pm

Re: Built in function library

Mon Oct 08, 2018 5:06 pm

Thanks. Commas it will be!

The date challenge is twofold:
1. Dates in the log file are not consistent. The date is excluded from entries for today and the year is missed out for entries this year. Ideally there would be a switch to set all log date/times to YYYYMMDDHHMMSS format. Machine readable and sortable.
2. If the router is set to anything else apart from GMT (ie there is a time offset), the log entries date/times go mad around midnight (GMT). Look at your log files when time is 00.00 GMT. I can only think it is a bug in ros. I'm on 6.43.2

Charles
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Mon Oct 08, 2018 5:38 pm

Well, I believe log format has nothing to do with built-in scripting functions...

About 00:00 bug (unfortunately, I don't have a log at 00:00, so I don't know what are you talking about) - have you contacted support@mikrotik.com?
 
howdey57
Member Candidate
Member Candidate
Posts: 137
Joined: Wed Dec 31, 2014 2:36 pm

Re: Built in function library

Mon Oct 08, 2018 5:46 pm

Chupaka

You are correct about log entries not directly being about functions but the flip side is that you need lots of other functions to be built to manipulate the inconsistent log date formats.

I'll try support@.

Thanks

Charles
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Built in function library

Wed Oct 17, 2018 4:47 pm

Secure DNS implementation would be welcome in the future
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Thu Oct 18, 2018 11:51 am

Secure DNS implementation would be welcome in the future
What scripting functions do you mean?..
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Built in function library

Thu Oct 18, 2018 12:23 pm

Hallo Chupaka, something like that.
In the same way the SSL turns HTTP web traffic into HTTPS encrypted Web traffic, DNSCrypt turns regular DNS traffic into encrypted DNS traffic that is secure from eavesdropping and man-in-the-middle attacks.

Grt
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Thu Oct 18, 2018 12:29 pm

You mean, some function like ":resolve", but using DNSCrypt protocol?
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Built in function library

Thu Oct 18, 2018 12:30 pm

Yes indeed that's what i mean Chupaka.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Thu Oct 18, 2018 1:13 pm

Completely unrelated to original topic.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Oct 18, 2018 4:11 pm

Completely unrelated to original topic.
What is the progress on the original topic? Has it been decided if this is going to happen, when, and what functions?
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Built in function library

Tue Apr 23, 2019 5:24 pm

Completely unrelated to original topic.
What topic is unrelated?
 
doush
Long time Member
Long time Member
Posts: 665
Joined: Thu Jun 04, 2009 3:11 pm

Re: Built in function library

Tue May 14, 2019 11:44 am

API responses are not Django Framework Web Template friendly for example Django cannot parse '-' .

For example api(cmd='/ip/dhcp-server/lease/print') returns the following
({'.id': '*213', 'address': '192.168.10.57', 'mac-address': 'F5:DE:F1:24:DC:20', 'client-id': '1:f0:de:f1:23:dc:2a',}) 
.id cannot be a key starts with a dot.
mac-address cannot be a key because it has '-'
same for client-id
We need a function to replace the '-' with '_' underscore and remove the dots as well to make it work with modern web frameworks.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Tue May 14, 2019 11:56 am

You can replace any symbol in your application.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Wed Jul 24, 2019 12:21 am

Any news on this topic? We have not heard anything in a long time.
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Built in function library

Thu Jul 25, 2019 4:46 pm

With firewall address-list with blocked ip automatically add dns resolve with name.
Would be a nice asset
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Thu Jul 25, 2019 5:00 pm

and how exactly it is related to scripting functions?
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 997
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: Built in function library

Thu Jul 25, 2019 5:58 pm

[removed b/c off-topic]
Additionally to all the mentioned functions, I'd consider a basic set of IP calculations very helpful.

-Chris
Last edited by cdiedrich on Fri Jul 26, 2019 12:20 pm, edited 2 times in total.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Jul 25, 2019 7:33 pm

I'd love to see the terminal object-oriented.
Please stop the off-topic messages, and please delete them if they are yours.
(I will delete this when that happens)
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 997
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: Built in function library

Fri Jul 26, 2019 12:21 pm

I'd love to see the terminal object-oriented.
Please stop the off-topic messages, and please delete them if they are yours.
(I will delete this when that happens)
After re-reading my post I have to fully agree. Edited.
Thanks,
-Chris
 
jondavy
Member Candidate
Member Candidate
Posts: 143
Joined: Tue May 12, 2009 11:14 pm
Location: Brasil

Re: Built in function library

Wed Aug 14, 2019 1:12 am

the ability to scan ssid via station script, thus automating and choosing the signal-based ssid, ccq in a simple way with one function
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: Built in function library

Mon Aug 26, 2019 4:55 pm

Manipulate DATE and TIME.
With Time we can do +-wdhms until not cross >24h. After that we receive the prefix like 1w3d.
/put ("13:14:15" +1w2d23h59m59s)
1w3d13:14:14
With DATE "jan/01/2019" we cannot do any of this operation like +1d. My workaround is script to Crossing Midnight by using ROS internal scheduler and own rules. Example
This should be Built in ROS function and some part of it is implemented in scheduler. This works with LeapYear too.
/system scheduler add name=SiBTemp on-event={nothing } start-date=feb/31/2019;
put [/system scheduler get [find name=SiBTemp] start-date]
mar/03/2019
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: Built in function library

Sat Aug 31, 2019 8:31 pm

Please let us do
  • CTRL+L, means "clear / cls" - Clear Screen feature from CLI scripting.
  • Put color on text by esc char. Currently it's build-in onto
    interface wireless spectral-history wlan1 duration=2s
    but not published to us.
 
szalkerous
newbie
Posts: 36
Joined: Thu Jan 21, 2016 2:30 am
Location: NH, USA

Re: Built in function library

Tue Sep 24, 2019 8:52 pm

Not sure if already suggested, but JSON support would be absolutely amazing!!!!!

See: JParseFunctions for a kinda hacky (but extremely useful) workaround that really needs to be implemented natively!
 
mmorelie
just joined
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Built in function library

Thu Sep 26, 2019 12:02 pm

Hi,

I agree with everything that has been said before. I also support the proposal made at the beginning of MD5Hash because sometimes the download of RouterOs or configuration file fails.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Wed Oct 02, 2019 3:08 pm

- a set of functions to support accessing another RouterOS device via API
 
mmorelie
just joined
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Built in function library

Thu Mar 12, 2020 10:55 am

hi,

possibility to create variables named from object on the routeur like :
:varname [:caps-man remote-cap get $i serial]
so i have a variable named BF090FS8938 (serial number of the router)
/env print
BF090FS8938={foo="bar"; foo; bar}
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1092
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Built in function library

Thu Mar 12, 2020 11:48 am

hi,

possibility to create variables named from object on the routeur like :
:varname [:caps-man remote-cap get $i serial]
so i have a variable named BF090FS8938 (serial number of the router)
/env print
BF090FS8938={foo="bar"; foo; bar}
You could put this into an array...
[admin@mt] > :global RemoteCap [ :toarray "" ];
[admin@mt] > :set ($RemoteCap->[ /caps-man remote-cap get $i serial ]) {"foo"="bar"; "bar"="testing..."}
[admin@mt] > :put ($RemoteCap->[ /caps-man remote-cap get $i serial ]->"foo")                                                  
bar
[admin@mt] > :put ($RemoteCap->"BF090FS8938"->"bar")
testing...
Does that help?
 
mmorelie
just joined
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Built in function library

Thu Mar 12, 2020 12:33 pm

I know but the variable doesn't have the serial number as name, here the variable is RemoteCap.
Sometimes it could be usefull.
 
ohilton576
just joined
Posts: 6
Joined: Sat Oct 20, 2018 10:17 pm

Re: Built in function library

Fri Apr 17, 2020 9:35 pm

String comparison would be great!
I am trying to sort my queue by name so I can group similarly named queue items in IP cluster ranges for further processing, but I am getting the dreaded "Script Error: cannot compare if string is less than string"
I can't even convert first character to some kind of number...

:if ([:tonum [:pick $strQueueName 0 1]] < [:tonum [:pick $strQ2CompareWith 0 1]]) do={....}
 
User avatar
macsrwe
Forum Guru
Forum Guru
Posts: 1011
Joined: Mon Apr 02, 2007 5:43 am
Location: Arizona, USA
Contact:

Re: Built in function library

Sun Apr 19, 2020 1:01 pm

You can if you name your queues with a first character that is a digit, otherwise you would have to do :find of a collation string. There's no function that returns the raw byte value of an arbitrary element.
 
pakillo
just joined
Posts: 8
Joined: Sun Sep 13, 2009 12:14 am

Re: Built in function library

Sun Jun 07, 2020 11:47 am

Just give us a bash
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Sun Jun 07, 2020 3:21 pm

Replace or remove character(s) in a string.
{
 global replaceCharacterFunc do={
  while condition=[find $1 $2 0] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
  return $1
 }
# Replace a character with an other character
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-" "*"]
# Remove unwanted character(s)
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-"]
}
Correct that the first character in the string could also be the one that could be replaced:
while condition=[find $1 $2 0] do={
Update: added matching on multiple chars and replace those with one or more chars.
{
 global mreplaceCharFunc do={
 :local n [:len $2]
  while condition=[find $1 $2 0] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+$n) ([len $1])]")}
  return $1
 }
}

:set $a [$mreplaceCharFunc "this @@@ @@ @ test of replacing multiple char(s)" @@ is]; :put $a 
this is@ is @ test of replacing multiple char(s)
Last edited by msatter on Wed Jul 12, 2023 3:26 pm, edited 2 times in total.
 
jondavy
Member Candidate
Member Candidate
Posts: 143
Joined: Tue May 12, 2009 11:14 pm
Location: Brasil

Re: Built in function library

Sun Jun 14, 2020 9:20 pm

Abilities with script to scan scan and connect into SSID with best signal when SSID start with criteria example:
This is a very important facility for field installations because the technician will not need to link or the notebook will be expedited or serviced, as is the case with GPON (expedited installation). example:
/interface wireless set [find default-name=wlan1] scan-and-connect = based-best-signal-ssid = [find=IspXSectorY];
Ability of AP to announce for all stations that there will be a nv2-mixed-background-scan-from-all of freq-usage from all stations and AP, review graphic for AP and mix as scan of all based at a time ex: 04:00am and temporarily save the RAM into the AP,
of the month that had to stop or painel for some seconds, we would assimilate a scan more from all clients and that would work even for AC chips Also,
when someone request a scan from or AP, warn them that they have scan and show a graph. example:
/interface wireless set [find default-name=wlan1] set nv2-mixed-background-scan-from-all=enabled nv2-mixed-background-scan-time = 04: 00am
 
vortex
Forum Guru
Forum Guru
Posts: 1130
Joined: Sat Feb 16, 2013 6:10 pm

Re: Built in function library

Mon Jun 15, 2020 12:03 am

Just give us a bash
I prefer scsh.
 
User avatar
s3RYjUr9ABp4
just joined
Posts: 4
Joined: Mon Aug 03, 2020 8:48 am

Re: Built in function library

Mon Aug 03, 2020 9:21 am

Need.
Lua script.
 
mike548141
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Sun Aug 16, 2020 5:14 am

Re: Built in function library

Sun Aug 16, 2020 5:20 am

To me the singularly most important addition would be opening this up to the community so that they could create and share functions safely and easily. So that you can leverage all the developers to contribute to a user extensible library.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Tue Aug 18, 2020 4:18 pm

To me the singularly most important addition would be opening this up to the community so that they could create and share functions safely and easily. So that you can leverage all the developers to contribute to a user extensible library.
You can always start such a project or join an existing one, e.g. on github.
Don't expect this from MikroTik, they are not an open-source company!
 
User avatar
Mannsean
just joined
Posts: 6
Joined: Thu Aug 13, 2020 3:32 pm
Location: England London

Re: Built in function library

Fri Aug 28, 2020 1:55 am

To me the singularly most important addition would be opening this up to the community so that they could create and share functions safely and easily. So that you can leverage all the developers to contribute to a user extensible library.
Just as said above, i think you have more chances with it on github
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Sat Sep 12, 2020 11:47 pm

A built-in function that broke up files and variables into 4096 byte chunks would be extremely useful.
I think the scripting language should be extended with a file open/read/write/close construct where you can open a file (a handle is returned), then read or write it in a loop either line-by-line or as fixed-size blocks (up to 4096 bytes), and then close it when finished. The total size of the file would be arbitrary (as long as it fits on the storage).
This would be similar to the C functions fopen, fgets, fputs, fread, fwrite, fclose.
This would allow reading and processing long lists of data like blacklists etc.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Thu Sep 17, 2020 1:27 am

Even kind of ":foreach line in=[/file xxx get-lines] do={$process $line}" should be enough, I believe...
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Sep 17, 2020 11:10 am

Ok, that would likely read the entire fire beforehand and store it into an array in memory, but indeed for many purposes (and with today's typical RAM size) that would already be very good!
Of course it would be nice when there also was some way to write-out such an array to a file (1 array element per line, total array larger than 4K).
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Thu Sep 17, 2020 12:03 pm

What if you use multiple array's in the foreach? Till now I read/stores up to 64KB files using one array.
When one array is full then switch to the next one.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Sep 17, 2020 12:27 pm

Oh, I did not know that there was a 64kB limit on arrays.
When that is the case, forget about that method and just do the open/read/write/close. It has no such limits, it can loop to read all lines and process them without storage limit issues.
(reading and processing lines one by one does not necessarily mean they have to be permanently stored somewhere in memory, maybe it is just to add them to an address list or similar purposes)
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Thu Sep 17, 2020 12:48 pm

This I am using to read up to 64KB from a file. Sadly always the first up to 64KB from a file.
 :local result [/tool fetch url=$url as-value output=user]; :if ($result->"downloaded" < 64) do={ :local data ($result->"data")
If a file is bigger then that, then the result is not transferred to the array due to the data is incomplete. This could work also for bigger files if the webserver supports resume and Mikrotik implements a new starting point for downloading the file using the resume option.

Quick-and-dirty:
Resume available: current read data is different from the previous data
No resume available: current read data is the same as the previous data

Edit: changed the check on the size of the downloaded file: :if ($result->"downloaded" < 64)
Last edited by msatter on Thu Sep 17, 2020 4:49 pm, edited 1 time in total.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Sep 17, 2020 1:56 pm

Does that already split the data on line boundaries into elements of the array? In that case it is equivalent to what Chupaka suggested to be added.
However, I prefer solutions that do not have such small arbitrary limits.
E.g. when I would like to download a file with lines with addresses or subnets, to add them to an address list, I would like to be able to process a file no matter if it is 10K 100K or 1M.
The open/read/close method does not have any inherent size limits, you can read the lines one by one in a loop, parse them to extract the interesting data, check if the item already is in the address list, if not, add it, and loop until all lines are read.
No need to fiddle with "if larger than this then that".
(I also use the loading of address lists via DNS but it has silly limits as well, you cannot load like 10000 items this way. and it also does not support subnets)
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Thu Sep 17, 2020 3:31 pm

I remember they promised a possibility to use routing filters to add/fetch routes to/from address lists in v7. That would probably be better solution for this case...
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Sep 17, 2020 4:08 pm

For this to be of any use, first two changes have to be made:
- address list from DNS should allow subnets, via a TXT record containing CIDR subnet notation (in addition to current A and AAAA record support)
- address list from DNS should have no unreasonable size limits, i.e. when I can "dig" the DNS name from a Linux shell, it should also be able to load that into an address list.
e.g.: dig -t A (some domain name) | wc -l
3546
cannot currently load this into an address list...
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Thu Sep 17, 2020 8:18 pm

Oh, I forgot to mention BGP... I mean, with that routing filter one may distribute address list (with subnets and whistles) via BGP, not using files...
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Thu Sep 17, 2020 9:37 pm

Ok, I thought you meant to use address lists as part of routing filters (the other way around) and then distribute those lists via DNS...
 
SimonThomasen
newbie
Posts: 32
Joined: Thu Apr 05, 2012 12:46 am

Re: Built in function library

Sun Oct 25, 2020 8:40 pm

read+Write 1 line of text, append to text file, to not have to read/write the whole file
 
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: Built in function library

Tue Oct 27, 2020 6:43 pm

Catch error messages
 
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: Built in function library

Tue Oct 27, 2020 6:45 pm

micro python :)
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Tue Oct 27, 2020 9:54 pm

Catch error messages
You mean, in here? https://wiki.mikrotik.com/wiki/Manual:S ... ime_errors
 
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: Built in function library

Tue Oct 27, 2020 11:16 pm

Catch error messages
You mean, in here? https://wiki.mikrotik.com/wiki/Manual:S ... ime_errors

Que? That just catches "an" error. There is no way to get the actual error message ie type of error.
 
doof
just joined
Posts: 8
Joined: Wed May 27, 2020 4:37 am

Re: Built in function library

Sun Aug 22, 2021 4:35 am

A function to extract the length field of an ip-prefix or ip6-prefix value.
:global prefixLen do={ return [:tonum [:pick $1 ([:find $1 "/"]+1) [:len $1] ] ] }
Usage:
/ip route print detail where active and bgp and [$prefixLen $"dst-address"]>24 and [$prefixLen $"dst-address"]<32
It's a little inefficient to resort to string manipulation here when the value already internally exists as an int, but there seems to be no built-in way to access it.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Sun Aug 22, 2021 12:09 pm

There has not been any sign of a built-in function library in the 3 years past this "We are considering to add commonly used functions as built-in" so it appears to become more and more useless to add requests to this topic.
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Sat Oct 02, 2021 3:02 pm

While we wait for the builtin functions:

https://github.com/merlinthemagic/MTM-R ... -Scripting

Can manipulate strings, files, md5 hashing, create GUIDs etc.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: Built in function library

Sat Oct 02, 2021 3:08 pm

While we wait for the builtin functions:

https://github.com/merlinthemagic/MTM-R ... -Scripting

Can manipulate strings, files, md5 hashing, create GUIDs etc.
You show a scripts without descripton what they do... I check code and no details even in it.
If someone must try your all scripts and build own info what each script do...then I even not try use them.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Built in function library

Sat Oct 02, 2021 3:16 pm

@SiB is another one that reinvent the wheel...

Is an hell to follo all dependencies, also for debug, and if something is changed probably that breake some recurring dependance.

Each script must have all the functions writed inside, and all must be clear.

I prefer to provide Snippets, like on my signature, because any item is clear and do not depend to other scripts.
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Sat Oct 02, 2021 3:34 pm

.....
You show a scripts without descripton what they do... I check code and no details even in it.
If someone must try your all scripts and build own info what each script do...then I even not try use them.
Try the documentations folder, the vast majority of functions / tools are documented.

Also, I posted a link to github at 01:02pm and a whole 6 minutes later (by 01:08) the code base has been vetted by a few individuals, who then follow me around as i am promoting the lib, replying to every post.
There are some strange cats on this forum....
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Built in function library

Sat Oct 02, 2021 3:52 pm

Miao? (Italian Meow)

P.S.: The forum is continuosly scanned from Google, Bing and others, every link is followed from the spider bots...
Is probable that all latest user (for sure except one) are bots....
Baidu [Spider]
Bing [Bot]
Google [Bot]
Google Feedfetcher
etc.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: Built in function library

Sat Oct 02, 2021 4:31 pm

Try the documentations folder, the vast majority of functions / tools are documented.
Miał,
https://github.com/merlinthemagic/MTM-R ... ces/LTE.md
and others not give anything. Case is close.
 
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: Built in function library

Sat Oct 02, 2021 8:32 pm

While we wait for the builtin functions: https://github.com/merlinthemagic/MTM-R ... -Scripting. Can manipulate strings, files, md5 hashing, create GUIDs etc.

Great stuff, thank you! Forked right away. Here is a link to mr Merlinthemagic's other work: https://github.com/merlinthemagic. Thanks Martin!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Mon Nov 29, 2021 10:19 pm

Not sure if already suggested, but JSON support would be absolutely amazing!!!!!

See: JParseFunctions for a kinda hacky (but extremely useful) workaround that really needs to be implemented natively!
THIS
+1 from me.

Especially with the REST API support in v7, which suggest you can use:
any other HTTP client even RouterOS fetch tool.

Perhaps a
:tojson
that take ROS script array type, and return a JSON string. Since :toarray already takes a string type,
something like
:jsonparse
that take a string with JSON, and return an ROS key-value array type.

Since the ROS REST API using JSON, while callable using /tool/fetch is possible, parsing the results involves a lot of work.

e.g. Let's assume this JSON was returned from /tool/fetch:
{
	"topkey": "topvalue",
	"children": {
		"oldest": "Jimmy",
		"youngest": "Jane"
	}
}
ROS array syntax actually pretty good at multilevel levels arrays (and already typeless), so if we assume a
:jsonparse
, it would look something like this:
:local jsonarr [:jsonparse [/tool/fetch ... as-value]]
:put $jsonarr
children=oldest=name=Jimmy;youngest=name=Jane;topkey=value
:put ($jsonarr->"children"->"oldest"->"name")
Jimmy
While perhaps there be limits to how much JSON can be converted (e.g. max lengths, int/float limits, etc). ROS arrays would seem to work in most cases to represent JSON.

XML, be tougher...Only thought is if array support the CAPsMAN "dot attributes", since could be attributes and children in XML but not in ROS array.
 
karolkaz
just joined
Posts: 2
Joined: Fri Dec 10, 2021 11:42 am

Re: Built in function library

Fri Dec 10, 2021 2:45 pm

I'm having trouble locating the library. Is it up yet?
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Fri Dec 10, 2021 10:20 pm

No... in 2018 this was under consideration, but it never came to be.
That is, from MikroTik. There are some library routines published by end-users on their sites and/or github repositories.
 
karolkaz
just joined
Posts: 2
Joined: Fri Dec 10, 2021 11:42 am

Re: Built in function library

Mon Dec 13, 2021 8:20 am

Oh, thanks for the answer!
 
max232
just joined
Posts: 7
Joined: Fri Apr 24, 2020 11:17 pm

Re: Built in function library

Mon Jan 03, 2022 2:51 pm

local a ( 10 / 3 ); log info $a
Please make a division)
the result should be of type 3.33333333
Not an integer: 3

Writing / reading an array to a file is also necessary.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Mon Jan 03, 2022 11:01 pm

Just wondering: where do you need this type of division?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7185
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Built in function library

Wed Jan 05, 2022 12:23 pm

In RouterOS number is an integer.

Integer division yields an integer result. You can use the remainder operator, %, which yields the remainder after integer division. Thus, 10 % 3 yields 1.
 
xenuc
just joined
Posts: 3
Joined: Mon Mar 02, 2020 8:28 am

Re: Built in function library

Wed Jan 05, 2022 8:57 pm

JSON encode and parse please!
 
Frozer
newbie
Posts: 48
Joined: Wed Apr 10, 2013 4:14 pm
Contact:

Re: Built in function library

Thu Jan 20, 2022 9:39 am

1. uppercase, lowercase
2. str_replace
3. Access a variable by value from another variable
4. break cycles

What I remembered. I will add the topic to bookmarks, I will add it in fact when I write the next script.

By the way, when will you expand the window for inserting scripts through WinBox? Many of my scripts do not fit in it and I go through "7 circles of hell" to add... o need to add include function. Time passes, memory is added, processors are more powerful, but the limitations are the same ...
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Built in function library

Thu Jan 20, 2022 5:56 pm

1) Most of my personal issues with ROS scripting are when it comes to string parsing and string manipulation. Gives how ROS scripting is used, these are some of the most used functions, and currently they lack of lot of utility functions.

:strleft - return a number of characters of the string (from the left)
:strright - return a number of characters of the string (from the right)
:strreplace - replace some characters in string

These would be super useful to avoid having to deal with "find and :pick all the time for simple string manipulation. And replace is just missing completely.

2) Next, the loops in ROS scripting are completely missing functionality to affect their code-flow.

:break - break out of a :for and :foreach, etc. loops
:continue - the usual concept of :continue in loops

3) Time and date manipulation. This is just not handled at all. For example, this script shows how to nicely deal with time/date increases: viewtopic.php?f=9&t=127294. Having the ability to natively do time/date manipulation would super useful. Here is an example using the function linked above:
:local targetDate [$addDate date=[/system clock get date] days=3]

I would much prefer a :todate function which would format a string as a date, and allow for easy operations. Something like this working natively would be lovely:
:local targetDate ([/system clock get date] + [:todate "3d"])

4) Finally, I would really like to get access to why an error occurred in on-error blocks. Here is what I would love:
:do {  
    # things
} on-error e do={
  :put "Failed, reason: $e"
};
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Thu Jan 20, 2022 6:43 pm

Replace and remove in string: viewtopic.php?p=798672#p798672
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Thu Jan 20, 2022 10:20 pm

To follow up on @tomaskir great suggestions...

1) [...] string parsing and string manipulation. [...] would be super useful to avoid having to deal with "find and :pick all the time for simple string manipulation. And replace is just missing completely.

One alternative to adding a bunch of new :strX things, is full support regular expressions, specifically capture groups and replace. Thought is a ":regex" command might be able to encompose a lot of use cases. Idea being it would return an ROS array with the "matches" returned from the regex groups defined in the match= regular expression provided, based on data in from=
:global mystr "blah, blah, blah"
:global matches [:regex match=/blah/ from=$mystr  multiline greedy global]
:put [:typeof $matches]
# array
:put $matches                       
# blah;blah;blah
:put [:len $matches] 
# 3

For string substitution,
:global replacedstring [:regex find="blah"  replace="hello" from=$mystr]
:put [:typeof $replacedstring]
# string

Basically have some light version of "sed" and "awk" be nice. Regex isn't for the faint of heart, but neither is BGP which supports regex in V7. Thinking it at least be cleaner script code than "[:strright [:strleft $x] 3" .


2) Next, the loops in ROS scripting are completely missing functionality to affect their code-flow.

:break - break out of a :for and :foreach, etc. loops
:continue - the usual concept of :continue in loops
Yup this comes up which then requires more complex conditional logic to avoid needing it...


3) Time and date manipulation. This is just not handled at all.
Here perhaps another possible simplification is a wrapper around GNU date, so perhaps ":datestr" that wraps it? The "modern" date in Linux support some humanizing options that pretty handy in /bin/sh script, see this gdate cheatsheet for example.


The options just follow the man pages, except with the ROS "=" method, so follow Bash shell usage a Linux date:
# returns the "7th day of last month"  
bash> gdate --date "-1 month" +"%Y-%m-07" 
# 2021-12-07
be the following in ROS, with added format=
[user@ROS]> :datestr date="-1 month" format="+\"%Y-%m-07\""
# 2021-12-07

As more sophisticated version of ":datestr" also have an "as-value" that get the current Mikrotik "time" type (e.g. weeks ago from 1970-epoch time).


4) Finally, I would really like to get access to why an error occurred in on-error blocks. Here is what I would love:
:do {  
    # things
} on-error e do={
  :put "Failed, reason: $e"
};

That's a good one, and since "$e" likely be a string, a "simple string find" operation from your 1 above is also needed with it ;).
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Built in function library

Fri Jan 21, 2022 12:39 am

4) Finally, I would really like to get access to why an error occurred in on-error blocks. Here is what I would love:
:do {  
    # things
} on-error e do={
  :put "Failed, reason: $e"
};

That's a good one, and since "$e" likely be a string, a "simple string find" operation from your 1 above is also needed with it ;).

Yes, $e could be a string. RouterOS commands already output an error message (string) when they fail, having access to that would be lovely for debug purposes. You could however easily imagine a more complex usecase. $e could be a more elaborate object, which could give you access to more failure data. For example:

$e->errorMessage - contains the error message string
$e->command - command which caused an error, useful if multiple commands in :do block
$e->lineNumber - on which line in the script the issue occured
etc.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Fri Jan 21, 2022 12:47 am

The problem with line/pos number reporting is that all scripts are first compiled into a P-code (a stack language) which is then executed.
By the time an error occurs in some function, the processor does not know to what source line this corresponds (both text and linenumber).
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Fri Jan 21, 2022 1:48 am

The problem with line/pos number reporting is that all scripts are first compiled into a P-code (a stack language) which is then executed.
By the time an error occurs in some function, the processor does not know to what source line this corresponds (both text and linenumber).
Not to be pedantic, but that's not quite true in all contexts. Certainly System>Script/Schedule and functions created using :global get converted into the
 \(evl(...
stuff on save/:set. But in the context of an "import", ROS seem to know the context of the line number being executed.
import verbose=yes
Now even that doesn't help with functions. An error in a function, will only show the line number of the calling location, not the line number of the error with the function's code.

But on-error with string – which seems readily knowable since it's printed to terminal – be a step in the right direction.

Since it's a new year, I'll add another plea for
JSON encode and parse please!
Ideally with JSON to/from ROS array support.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Fri Jan 21, 2022 10:33 am

The problem with line/pos number reporting is that all scripts are first compiled into a P-code (a stack language) which is then executed.
By the time an error occurs in some function, the processor does not know to what source line this corresponds (both text and linenumber).
Not to be pedantic, but that's not quite true in all contexts. Certainly System>Script/Schedule and functions created using :global get converted into the
 \(evl(...
stuff on save/:set. But in the context of an "import", ROS seem to know the context of the line number being executed.
For language syntax errors that are detected while compiling the script the line number when it is encountered is known.
But when a syntactically correct script encounters a runtime error during execution, the line number (and character position) of the original source line isn't known.

It is like using a compiled language like C on a computer. When you compile the code, the compiler shows the line number of your syntax errors.
But when you run it and encounter an error like division by zero or dereferencing a NULL pointer, the line number isn't printed with the error.

Of course it would be possible to introduce "debugging info" in the form of special instructions that maintain a "current line number" register in the P-code interpreter that is printed when a runtime error occurs, but it is extra effort and it has not been done.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8712
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Built in function library

Fri Jan 21, 2022 9:07 pm

o need to add include function.
Declare your functions in a script, then execute that script from another script to "include" necessary functio(s): https://wiki.mikrotik.com/wiki/Manual:S ... #Functions
 
User avatar
jr0dd
just joined
Posts: 14
Joined: Fri Feb 10, 2017 4:46 am

Re: Built in function library

Thu Jan 27, 2022 6:31 am

json parsing seems to be the consensus in here and definitely has my vote. or maybe on devices with more storage just give us the ability to use bash, curl & jq :shock:
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Tue Feb 15, 2022 12:49 am

While this seems like nascent, or at least low moving, topic... Did run into another missing built-in function: zip/unzip and/or tar/gzip support.

The container package seems internally deal with TAR files, so be nice if this was exposed for scripting. Basically be handy to some :compress and :deflate function. Or, perhaps an option to /file that do similar.
 
giulianoz
newbie
Posts: 27
Joined: Sat Apr 08, 2017 6:44 pm

Re: Built in function library

Mon Aug 15, 2022 11:12 am

I too would love to see curl/jq as built-ins :)
 
mada3k
Forum Veteran
Forum Veteran
Posts: 740
Joined: Mon Jul 13, 2015 10:53 am
Location: Sweden

Re: Built in function library

Sun Jan 15, 2023 5:43 pm

Creating a new file and appending input from CLI. Adding a new public ssh key as file, and importing it to a user. Today it has to be done via SCP/SFTP, then imported.

For example:
/file add name="user.pub" content="ssh-rsa XXXXXXXXXXXXXXXXXXXXX"
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Built in function library

Mon Jul 17, 2023 4:01 pm

We are considering to add commonly used functions as built-in.
What functions would you like to see?
For example, MD5hash, random number generator and so on.
Please fix the "third function call" error at last. For example, we have a function:
:local myFunc do {:log info "Hello world !"}
If we call it with the [] shell twice in a row, it will be executed three times!
[$myFunc]
[$myFunc]
Hello world !
Hello world !
Hello world !


If we make a call without a wrapper [], this error does not occur:
$myFunc
$myFunc
Hello world !
Hello world !

If the function is called to perform :put, there will be gaps:

viewtopic.php?t=197800

Interestingly, subsequent calls after the second are processed normally:
[$myFunc]
[$myFunc]
[$myFunc]
Hello world !
Hello world !
Hello world !
Hello world !
 
User avatar
Sertik
Member
Member
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Built in function library

Mon Jul 17, 2023 4:13 pm

As for the built-in functions, I will refrain from speaking for now, but what I would like is that it would be possible to create sections in the script repository, so that it would be possible to store scripts not in a common huge list. After all, the number of scripts in the repository of a modern router can be impressive. And you could call it by specifying the path
/system/script/myScript1/run script1
/system/script/myScript2/add script2
/system/script/myScript1/remove script1

Yes, this will entail the need to rewrite many scripts, but now the situation is the best for introducing folders into the repositories, when many scripts are already being rewritten for Ros7. Yes, it's not clear how to make scripts located in different sections see and be able to run each other ... But, probably, this could be somehow solved. this should have been done from the very beginning.

In the end, for the system, the scripts, as they were stored, can be stored in one common section, but for the user they could be seen in separate subsections / folders

It would also speed up script browsing in Winbox when outputting a section of say 15 scripts is much faster than outputting 500.
 
mike548141
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Sun Aug 16, 2020 5:14 am

Re: Built in function library

Mon Jul 24, 2023 11:19 am

Some ideas
Syntax checking
A big one for me would not be a new function, but making scripting easier across everyting using the Mikrotik platform.
RouterOS on a CHR virtual device does not have /system/routerboard/ in the CLI so a script will give a syntax error when it trys to load the script into memory i.e. pre-execution. Similarly most routerboard hardware devices do not have /system/hardware/ so they will give a syntax error on load if that is referenced anywhere in the script file.
The script has logic to avoid running the wrong commands on certain devices but the script will not execute because it will fail syntax checking. Currently I get around this by parsing the commands in dispute so they do not get validated until they are executed, but frankly thats pretty ugly.

The simple answer to me (but there are probably better ideas) would be to have the same CLI structure irresepctive of the device. For example I can call :put [ /system/routerboard/get routerboard; ]; and get a false if it were a CHR.

Filesystem
Some basic file and directory handling like mkdir, I work around that using /tool fetch but would prefer not to have too.

Ta
Last edited by mike548141 on Tue Jul 25, 2023 1:11 pm, edited 1 time in total.
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Mon Jul 24, 2023 12:20 pm

@mike548141 When you have commands that could not work on every device then you can avoid errors by using do {} on-error={}
:do { /system/hardware } on-error={ :error "error: script not executable on this device " }

:do { /system/hardware } on-error={ :log "error: script X not executable on this device " }
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Mon Jul 24, 2023 6:17 pm

Filesystem
Some basic file and directory handling like mkdir, I work around that using /tool fetch but would prefer not to have too.
There is now "/file/add name=" in the latest V7's. Directories kinda fake in RouterOS as they are constructed from the "/" in the name= .... so still need to create some file. But /file/add avoids awkward syntax, still not "mkdir" ... but better than /tool/fetch for this.

Now they could add a "/file/add name=mydir type=directory" that least get what looks to be a empty directory in winbox and /file/print. But... directory are automatically created whenever you need a file, without any need for them to exist (e.g. why no "mkdir").
 
mike548141
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Sun Aug 16, 2020 5:14 am

Re: Built in function library

Tue Jul 25, 2023 12:58 pm

@mike548141 When you have commands that could not work on every device then you can avoid errors by using do {} on-error={}
:do { /system/hardware } on-error={ :error "error: script not executable on this device " }
:do { /system/hardware } on-error={ :log "error: script X not executable on this device " }
Hi msatter

In my testing that would still cause a syntax error, you need :parse to get past the syntax checker. For example on a CHR running 7.10.2 I get
:do { /system/routerboard/print } on-error={ :error "error: script not executable on this device " }                         
syntax error (line 1 column 26)
To get around it I use a little function... but its ugly
:global fsynnav do={
  :local lsyncmd [ :parse $cmd; ];
  :do {
    :return [ $lsyncmd; ];
  } on-error={
    :put ("Syntax handler had a problem running a command, this hardware or RouterOS version probably does not support the command " . $cmd);
  };
};

$fsynnav cmd="/system/routerboard/print"
 
mike548141
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Sun Aug 16, 2020 5:14 am

Re: Built in function library

Tue Jul 25, 2023 1:06 pm

Filesystem
Some basic file and directory handling like mkdir, I work around that using /tool fetch but would prefer not to have too.
There is now "/file/add name=" in the latest V7's. Directories kinda fake in RouterOS as they are constructed from the "/" in the name= .... so still need to create some file. But /file/add avoids awkward syntax, still not "mkdir" ... but better than /tool/fetch for this.

Now they could add a "/file/add name=mydir type=directory" that least get what looks to be a empty directory in winbox and /file/print. But... directory are automatically created whenever you need a file, without any need for them to exist (e.g. why no "mkdir").
Thanks Amm0 I had not noticed that. Going to amend my script now! As you say thats better than /tool/fetch
 
mike548141
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Sun Aug 16, 2020 5:14 am

Re: Built in function library

Tue Jul 25, 2023 1:50 pm

Other ideas...
if...else if...else
It would be great to have the option to use elif (else if) within a if statement rather than nesting new if statements within a else.
:if ($something = "abc") do={
  :put "Thing 1";
} elif ($something = "def") do={
  :put "Thing 2";
} else={
  :put "No Things here";
};
Case statements
The ability to use case statements for multi branch statements
switch ($day) do={
  case 0 do={
    :put "Sun";
  };
  case 1 do={
    :put "Mon";
  };
  case 2 do={
    :put "Tues";
  };
};
Waiting for execution to complete
Some commands take a moment to finish executing (e.g. /system/check-installation) and the script doesn't know when its complete. I work around that using :execute and monitoring the file output but perhaps an exit status or something?

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

Re: Built in function library

Tue Jul 25, 2023 3:16 pm

Waiting for execution to complete
Some commands take a moment to finish executing (e.g. /system/check-installation) and the script doesn't know when its complete. I work around that using :execute and monitoring the file output but perhaps an exit status or something?

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

Re: Built in function library

Tue Jul 25, 2023 3:18 pm

Waiting for execution to complete
Some commands take a moment to finish executing (e.g. /system/check-installation) and the script doesn't know when its complete. I work around that using :execute and monitoring the file output but perhaps an exit status or something?
Other than /container start/stop, most command are synchronous (or can be made so). So the /system/check-installation just needs an "as-value":
:if (([/system/check-installation as-value]->"status")~"ok") do={
	:put "good"
} else={
	:put "bad"
}
 
msatter
Forum Guru
Forum Guru
Posts: 2941
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Built in function library

Tue Jul 25, 2023 7:53 pm

@mike548141 It is even simpler:
@> /system/hardware print
bad command name hardware (line 1 column 9)
@> /system/hardware/print 
syntax error (line 1 column 17)

Try
 /system/routerboard print
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Built in function library

Tue Jul 25, 2023 10:54 pm

about check something...
:global Dmsg ""
:global Gstop do={:put "";:put "*** INTERROTTO: $Dmsg ***";/co cl;:delay 10s;:put "";/quit}
:put "Controllo Installazione in corso..."
:global Gchkinst [/sys check-installation as-value]
:put "... Controllo Completato"
:if (($Gchkinst->"status")!="installation is ok") do={$Gstop Dmsg="INSTALLAZIONE CORROTTA: $Gchkinst"}
/sys back save dont-encrypt=yes password="" name=testmemory
:delay 4s
:if ([:len [/log find where message~"error creating backup file"]] > 0) do={
 /file remove [find where name~"testmemory.backup"]
 $Gstop Dmsg="ssh-host-key, NAND o FLASH memory danneggiata. Tentare risoluzione con: /ip ssh regenerate-host-key"
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Built in function library

Tue Jul 25, 2023 11:00 pm

@mike548141

For example, regardless RouterOS version, not all device have reset button / port-controller / port-extender, so on preconfiguration script:
:execute "/sys routerboard reset-button set enabled=no hold-time=0s..1m on-event=\"\""
:execute "/int bridge port-controller set bridge=none cascade-ports=\"\" switch=none"
:execute "/int bridge port-extender set control-ports=\"\" excluded-ports=\"\" switch=none"
 
patrickmkt
Member Candidate
Member Candidate
Posts: 202
Joined: Sat Jul 28, 2012 5:21 pm

Re: Built in function library

Wed Aug 02, 2023 7:14 pm

I would like to get an AES encryption function. Or an access to openssl functions.
 
PackElend
Member Candidate
Member Candidate
Posts: 273
Joined: Tue Sep 29, 2020 6:05 pm

Re: Built in function library

Wed Dec 27, 2023 5:31 pm

While we wait for the builtin functions:

https://github.com/merlinthemagic/MTM-R ... -Scripting

Can manipulate strings, files, md5 hashing, create GUIDs etc.
@merlinthemagic7 thx for the nice compilation of missing basic functions :D

I'm trying to understand the principle structure, reading:
  1. https://github.com/merlinthemagic/MTM-R ... e-examples
  2. https://github.com/merlinthemagic/MTM-R ... trings.rsc
Instead of writing single functions you put all in a big array and call the function an array item?
That is how In understand these extracts:
##trim string example
:local strTool [($MtmFacts->"get") "getTools()->getTypes()->getStrings()"];
:local myStr " My string with leading and traling spaces and line breaks and chr returns \n\r";
:put [($strTool->"trim") $myStr]; #string "My string with leading and traling spaces and line breaks and chr returns"
and
:set ($s->"trim") do={
	
	:global MtmFacts;
	:local cPath "MTM/Tools/DataTypes/Strings.rsc/trim";
	:if ([:typeof $0
...	
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Built in function library

Wed Dec 27, 2023 5:54 pm

the nil value is a pain
but it is worse to return useless errors, if not requested.

The scripts have some errors and something like this have a bad implementation:
# how is possible, at this pont, that $r0 is nil ???
	:if ($r0 = [:nothing]) do={
		#always return string, the nil value is a pain
		:set r0 "";
	}

Just simply correct the appropriate line
	:local p0 [:tostr $0];

make useless also
	:if ([:typeof $0] != "str") do={
		:error ($cPath.": Input has invalid type '".[:typeof $0]."'");
	}

and probably must check also \t [tab] (and rarely \A0 [no breaking space], \f [form feed] and \v [vertical tab]) not only "space" \r [carriage return] and \n [new line]...
 
MakroTok
newbie
Posts: 28
Joined: Wed Dec 04, 2019 4:36 pm

Re: Built in function library

Sun Mar 03, 2024 11:19 am

I'd love $0 to return the script name. This definetly needs to be a RouterOS functionality as one cannot work around on its own.
(Use case would be similar to busybox where one has one binary which behaves differntly depending on the name of the symbolic link used to call it.)

One could use e.g. the same script to stored by different names to handle various DDNS services.
 
MakroTok
newbie
Posts: 28
Joined: Wed Dec 04, 2019 4:36 pm

Re: Built in function library

Sun Mar 03, 2024 2:27 pm

I'd love $0 to return the script name. This definetly needs to be a RouterOS functionality as one cannot work around on its own.
(Use case would be similar to busybox where one has one binary which behaves differntly depending on the name of the symbolic link used to call it.)

One could use e.g. the same script to stored by different names to handle various DDNS services.
Just found out about
:jobname
seems to provide the script name already. :-o

Shame on me.
 
ohilton576
just joined
Posts: 6
Joined: Sat Oct 20, 2018 10:17 pm

Re: Built in function library

Fri Mar 29, 2024 1:57 pm

It would be great if there was ODBC connector function so we can write to external databases.
Or any other method of writing to external databases. Current methods are indirect, obscure, complex, inefficient and non-secure.
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Sat Mar 30, 2024 12:37 pm

While we wait for the builtin functions:

https://github.com/merlinthemagic/MTM-R ... -Scripting

Can manipulate strings, files, md5 hashing, create GUIDs etc.
@merlinthemagic7 thx for the nice compilation of missing basic functions :D

I'm trying to understand the principle structure, reading:
  1. https://github.com/merlinthemagic/MTM-R ... e-examples
  2. https://github.com/merlinthemagic/MTM-R ... trings.rsc
Instead of writing single functions you put all in a big array and call the function an array item?
That is how In understand these extracts:
##trim string example
:local strTool [($MtmFacts->"get") "getTools()->getTypes()->getStrings()"];
:local myStr " My string with leading and traling spaces and line breaks and chr returns \n\r";
:put [($strTool->"trim") $myStr]; #string "My string with leading and traling spaces and line breaks and chr returns"
and
:set ($s->"trim") do={
	
	:global MtmFacts;
	:local cPath "MTM/Tools/DataTypes/Strings.rsc/trim";
	:if ([:typeof $0
...	
Hi PackElend,

It s not one large array, but a bunch of global variables each containing an array. Corruption happens when an array gets too large. But in essense you are correct the functions are stored as items in an array.

The basic design is an attempt to mimic objects with the ability to call methods on those objects.

Everything starts with the $MtmFacts global. https://github.com/merlinthemagic/MTM-R ... /Facts.rsc
That is the only file you ever have to use the /import command to load as it will locate and load all other "classes".
/import flash/MTM/Facts.rsc;
:global MtmFacts;
Lets walk through your example.
##trim string example
:local strTool [($MtmFacts->"get") "getTools()->getTypes()->getStrings()"];

Here we are calling the "get" method on the $MtmFacts "object". That method takes a "class path" where the path is separated by "->", the factory will recurse each class until it gets to the final class you want. In this case the string tool:

getTools(): loads the tools factory. https://github.com/merlinthemagic/MTM-R ... /Tools.rsc

Then calls getTypes() method on the tools factory, which returns another factory: https://github.com/merlinthemagic/MTM-R ... /Types.rsc

Then calls getStrings() method on the types factory, which returns the tool singleton class: https://github.com/merlinthemagic/MTM-R ... trings.rsc

You could do the same as above, but using with individual calls like so:
:local toolFact [($MtmFacts->"getTools")];
:local typeFact [($toolFact->"getTypes")];
:local strTool [($typeFact->"getStrings")];

In both cases what you end up with is a variable ("object") called $strTool that you can now use to call methods on as many times as you like:
:put [($strTool->"trim") "    my string with spaces at each end  "]; ##trims the leading and trailing spaces
:put [($strTool->"getRandom") 64]; ##gives you a random string of length 64
:put [($strTool->"toLower") "MY strIng WiTH mixED cases"]; ##my string with mixed cases
You get the point. Helpful to have this implementation when doing loops and you need to e.g. trim each item in the loop.

All in all it is a way to organize tools we use all the time in a tree structure and separate each class into its own file. If you are familiar with OOP you will understand the factory pattern, readability, code reuse etc.

Its quite easy to add classes and methods, for example if you have built a class that can do SHA256:

Add another method to the Hashing factory called "getSHA256" (just copy "getMD5"):
https://github.com/merlinthemagic/MTM-R ... ashing.rsc

Add a file called SHA256.rsc in:
https://github.com/merlinthemagic/MTM-R ... ls/Hashing

Have a single method on your class called "get" (see MD5.rsc for example).

Submit a pull request and now everyone can hash strings with your method like this:
:local toolObj [($MtmFacts->"get") "getTools()->getHashing()->getSHA256()"];
:local myStr "My string";
:put ([($toolObj->"get") $myStr]); #3f9a07d83c604dba400d13df4d34566b78338804f0b3181d4e02089fe4daa7b0
Last edited by merlinthemagic7 on Mon Apr 01, 2024 8:44 am, edited 2 times in total.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Built in function library

Sat Mar 30, 2024 6:10 pm

I'd love $0 to return the script name.

It's this case where $0 being the containing array from a function be useful, :jobname does NOT help here:
But in essense you are correct the functions are stored as items in an array.

The basic design is to attempts to mimic objects and having the ability to call methods on those objects.
I've thought before that $0 should be a "this" pointer when a function is contained within an array. In array function, $0 is the first parameter so this has side-effects (see viewtopic.php?t=181142)

Since V7 copies arrays, if functions could access data within their OWN array, that the essence of an object.

But there is no way for a function to know what array contains it (e.g. where is "lives"), so no way to access data from other array members without some external variables.

Whether that's $0 or some $this available to "functions inside array's" is less important, it's that array function cannot access other array members directly is what limits more "real" objects (or even a C++-like vtable with more tricks).
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Mon Apr 01, 2024 9:11 am

I'd love $0 to return the script name.

It's this case where $0 being the containing array from a function be useful, :jobname does NOT help here:
But in essense you are correct the functions are stored as items in an array.

The basic design is to attempts to mimic objects and having the ability to call methods on those objects.
I've thought before that $0 should be a "this" pointer when a function is contained within an array. In array function, $0 is the first parameter so this has side-effects (see viewtopic.php?t=181142)

Since V7 copies arrays, if functions could access data within their OWN array, that the essence of an object.

But there is no way for a function to know what array contains it (e.g. where is "lives"), so no way to access data from other array members without some external variables.

Whether that's $0 or some $this available to "functions inside array's" is less important, it's that array function cannot access other array members directly is what limits more "real" objects (or even a C++-like vtable with more tricks).
Hi Amm0,

Generally, having the ability to build complex applications that run natively on ROS would be lovely, but im not sure the appetite is there from the MT team. They did inadvertently expose shared memory segments in v6 (viewtopic.php?p=948392#p948392) but shut the door on it with v7.

We opted for building a small ROS based agent that push/pull data from a cloud based management system rather than hope MT will expose the feature sets that let us go ROS native. There is obviously a latency penalty and a requirement of internet connectivity, but in return we can do the complex parsing + processing in an environment that has whatever tools are needed. The ROS agent just implements whatever its being told, rather than house any significant logic. Same goes for metrics, small agent that dumps data to a service for parsing.

Obvs I dont know you use case, and who knows what MT has in store, but so far these choices have served us well and has avoided us blocked waiting for a feature.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10529
Joined: Mon Jun 08, 2015 12:09 pm

Re: Built in function library

Mon Apr 01, 2024 11:57 am

When you need more powerful processing and do not want to use "the cloud" you could also consider running a container locally on the router (at least when the router is sufficiently equipped for that).
 
PackElend
Member Candidate
Member Candidate
Posts: 273
Joined: Tue Sep 29, 2020 6:05 pm

Re: Built in function library

Mon Apr 01, 2024 10:26 pm

a general question to scripts on https://github.com/merlinthemagic/MTM-R ... ain/src/v7.
The only global variable is
:global MtmFacts;
, anything else is local?
I'm only wondering if I could overwrite existing variables?
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Tue Apr 02, 2024 9:07 am

Hi,

Everything that stores a function is global, it has to be in order for different processes to share the loaded tools.

For an example see:
https://github.com/merlinthemagic/MTM-R ... ashing.rsc

While MtmFacts loads the Tools factory, the tools themselves are being stored in other globals e.g. in this case MtmTools.
:global MtmTools;
:set ($MtmTools->"hashing") $s;
As i explained earlier, it is necessary to chop up the globals or you get corruption. Point is you dont have to worry about that as long as you use the factories to load.

To see your environment check :
/system/script/environment/print
 
PackElend
Member Candidate
Member Candidate
Posts: 273
Joined: Tue Sep 29, 2020 6:05 pm

Re: Built in function library

Wed Apr 03, 2024 1:51 pm

As i explained earlier, it is necessary to chop up the globals or you get corruption. Point is you dont have to worry about that as long as you use the factories to load.
Hi,
I'm wondering which global variable the scripts are using.
Is there any variable that may call
$temp
, which is very likely that I use it in my self-developed scripts.
 
User avatar
merlinthemagic7
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Fri Sep 16, 2016 8:49 pm

Re: Built in function library

Thu Apr 04, 2024 2:27 pm

Hi,
I'm wondering which global variable the scripts are using.
Is there any variable that may call
$temp
, which is very likely that I use it in my self-developed scripts.
In the spirit of maintaining some type of name space separation, all Globals are prefaced with "Mtm". If $temp is being overridden, its not MTM doing it.
[dcnstaff@xxxxx.xxxxx.xxx] > /system/script/environment/print
Columns: NAME
 #  NAME         
6  MtmFaObjs    
 7  MtmFacts     
 8  MtmId        
 9  MtmIds1      
10  MtmIds2      
11  MtmLocks     
12  MtmModelIfs1 
13  MtmModels    
14  MtmToolJson1 
15  MtmToolParse1
16  MtmToolTime1 
17  MtmToolTypes1
18  MtmTools 
 
PackElend
Member Candidate
Member Candidate
Posts: 273
Joined: Tue Sep 29, 2020 6:05 pm

Re: Built in function library

Thu Apr 04, 2024 3:57 pm

In the spirit of maintaining some type of name space separation, all Globals are prefaced with "Mtm". If $temp is being overridden, its not MTM doing it.
GREAT :D :D :D

Who is online

Users browsing this forum: WarlorZ and 6 guests