Page 1 of 1

Built in function library

Posted: Thu Mar 08, 2018 4:56 pm
by mrz
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.

Re: Built in function library

Posted: Thu Mar 08, 2018 5:22 pm
by boen_robot
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.

Re: Built in function library

Posted: Fri Mar 09, 2018 8:52 am
by Joni
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)

Re: Built in function library

Posted: Mon Mar 12, 2018 5:15 pm
by boen_robot
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).

Re: Built in function library

Posted: Mon Mar 12, 2018 10:56 pm
by nz_monkey
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..

Re: Built in function library

Posted: Tue Mar 13, 2018 9:48 am
by Joni
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)

Re: Built in function library

Posted: Tue Mar 13, 2018 1:41 pm
by anav
Easy, a function that creates Ports/Services groups, which then can be used in port forwarding and firewall rules etc........

Re: Built in function library

Posted: Tue Mar 13, 2018 2:00 pm
by pe1chl
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!

Re: Built in function library

Posted: Tue Mar 13, 2018 7:01 pm
by ivanfm
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.

Re: Built in function library

Posted: Wed Mar 14, 2018 2:27 pm
by omega-00
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. :-)

Re: Built in function library

Posted: Thu Mar 22, 2018 12:01 pm
by thobias
Date/time comparison (NOW();) and option to get time in different formats.
Json parsing.
LUA or whatever to be able to parse larger files.

Re: Built in function library

Posted: Thu Apr 12, 2018 8:51 am
by armandfumal
url_encode();

Re: Built in function library

Posted: Wed Apr 18, 2018 1:55 pm
by Chupakabra303
LUA +1
Internal JSON parser to associative array variable (by type http://www.embest.ru/mikrotik/json-parser-script)

Re: Built in function library

Posted: Thu Apr 19, 2018 11:23 am
by thantoldo
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?

Re: Built in function library

Posted: Fri Apr 20, 2018 10:57 pm
by shdwmstff
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.

Re: Built in function library

Posted: Sat Apr 21, 2018 1:45 am
by pe1chl
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.

Re: Built in function library

Posted: Mon May 14, 2018 4:03 am
by telepro
+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

Re: Built in function library

Posted: Thu Jul 12, 2018 7:38 pm
by doneware
JSON encoder
regexp (PCRE) string manipulation

Re: Built in function library

Posted: Fri Jul 27, 2018 3:53 am
by Mantrid
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.

Re: Built in function library

Posted: Fri Aug 24, 2018 4:01 pm
by eworm
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

Re: Built in function library

Posted: Fri Aug 24, 2018 4:34 pm
by mrz
bitwise operator works, whats missing is :toipv6

Re: Built in function library

Posted: Fri Aug 24, 2018 5:25 pm
by eworm
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).

Re: Built in function library

Posted: Tue Aug 28, 2018 1:17 pm
by eworm
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

Re: Built in function library

Posted: Tue Aug 28, 2018 2:04 pm
by mrz
Yes it is not implemented.

Re: Built in function library

Posted: Mon Sep 17, 2018 11:09 am
by rendezz
url_encode();
+1 to this, my implementation is limited and awful

Re: Built in function library

Posted: Thu Sep 27, 2018 12:10 pm
by Usermikrotik
simulate a runtime command and send it to a running command in a script like in these commands

Image

Re: Built in function library

Posted: Mon Oct 08, 2018 11:23 am
by howdey57
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

Re: Built in function library

Posted: Mon Oct 08, 2018 11:35 am
by Chupaka
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
	};
}

Re: Built in function library

Posted: Mon Oct 08, 2018 11:48 am
by howdey57
Thanks. I'll try those. Are they mentioned in the wiki?

Any thoughts on the dates?

Re: Built in function library

Posted: Mon Oct 08, 2018 11:51 am
by Chupaka
Are they mentioned in the wiki?
Not sure :)
Any thoughts on the dates?
I didn't get what's wrong with dates, sorry

Re: Built in function library

Posted: Mon Oct 08, 2018 11:55 am
by eworm
- 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

Re: Built in function library

Posted: Mon Oct 08, 2018 5:06 pm
by howdey57
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

Re: Built in function library

Posted: Mon Oct 08, 2018 5:38 pm
by Chupaka
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?

Re: Built in function library

Posted: Mon Oct 08, 2018 5:46 pm
by howdey57
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

Re: Built in function library

Posted: Wed Oct 17, 2018 4:47 pm
by plisken
Secure DNS implementation would be welcome in the future

Re: Built in function library

Posted: Thu Oct 18, 2018 11:51 am
by Chupaka
Secure DNS implementation would be welcome in the future
What scripting functions do you mean?..

Re: Built in function library

Posted: Thu Oct 18, 2018 12:23 pm
by plisken
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

Re: Built in function library

Posted: Thu Oct 18, 2018 12:29 pm
by Chupaka
You mean, some function like ":resolve", but using DNSCrypt protocol?

Re: Built in function library

Posted: Thu Oct 18, 2018 12:30 pm
by plisken
Yes indeed that's what i mean Chupaka.

Re: Built in function library

Posted: Thu Oct 18, 2018 1:13 pm
by mrz
Completely unrelated to original topic.

Re: Built in function library

Posted: Thu Oct 18, 2018 4:11 pm
by pe1chl
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?

Re: Built in function library

Posted: Tue Apr 23, 2019 5:24 pm
by plisken
Completely unrelated to original topic.
What topic is unrelated?

Re: Built in function library

Posted: Tue May 14, 2019 11:44 am
by doush
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.

Re: Built in function library

Posted: Tue May 14, 2019 11:56 am
by mrz
You can replace any symbol in your application.

Re: Built in function library

Posted: Wed Jul 24, 2019 12:21 am
by eworm
Any news on this topic? We have not heard anything in a long time.

Re: Built in function library

Posted: Thu Jul 25, 2019 4:46 pm
by plisken
With firewall address-list with blocked ip automatically add dns resolve with name.
Would be a nice asset

Re: Built in function library

Posted: Thu Jul 25, 2019 5:00 pm
by mrz
and how exactly it is related to scripting functions?

Re: Built in function library

Posted: Thu Jul 25, 2019 5:58 pm
by cdiedrich
[removed b/c off-topic]
Additionally to all the mentioned functions, I'd consider a basic set of IP calculations very helpful.

-Chris

Re: Built in function library

Posted: Thu Jul 25, 2019 7:33 pm
by pe1chl
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)

Re: Built in function library

Posted: Fri Jul 26, 2019 12:21 pm
by cdiedrich
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

Re: Built in function library

Posted: Wed Aug 14, 2019 1:12 am
by jondavy
the ability to scan ssid via station script, thus automating and choosing the signal-based ssid, ccq in a simple way with one function

Re: Built in function library

Posted: Mon Aug 26, 2019 4:55 pm
by SiB
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

Re: Built in function library

Posted: Sat Aug 31, 2019 8:31 pm
by SiB
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.

Re: Built in function library

Posted: Tue Sep 24, 2019 8:52 pm
by szalkerous
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!

Re: Built in function library

Posted: Thu Sep 26, 2019 12:02 pm
by mmorelie
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.

Re: Built in function library

Posted: Wed Oct 02, 2019 3:08 pm
by pe1chl
- a set of functions to support accessing another RouterOS device via API

Re: Built in function library

Posted: Thu Mar 12, 2020 10:55 am
by mmorelie
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}

Re: Built in function library

Posted: Thu Mar 12, 2020 11:48 am
by eworm
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?

Re: Built in function library

Posted: Thu Mar 12, 2020 12:33 pm
by mmorelie
I know but the variable doesn't have the serial number as name, here the variable is RemoteCap.
Sometimes it could be usefull.

Re: Built in function library

Posted: Fri Apr 17, 2020 9:35 pm
by ohilton576
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={....}

Re: Built in function library

Posted: Sun Apr 19, 2020 1:01 pm
by macsrwe
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.

Re: Built in function library

Posted: Sun Jun 07, 2020 11:47 am
by pakillo
Just give us a bash

Re: Built in function library

Posted: Sun Jun 07, 2020 3:21 pm
by msatter
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)

Re: Built in function library

Posted: Sun Jun 14, 2020 9:20 pm
by jondavy
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

Re: Built in function library

Posted: Mon Jun 15, 2020 12:03 am
by vortex
Just give us a bash
I prefer scsh.

Re: Built in function library

Posted: Mon Aug 03, 2020 9:21 am
by s3RYjUr9ABp4
Need.
Lua script.

Re: Built in function library

Posted: Sun Aug 16, 2020 5:20 am
by mike548141
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.

Re: Built in function library

Posted: Tue Aug 18, 2020 4:18 pm
by pe1chl
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!

Re: Built in function library

Posted: Fri Aug 28, 2020 1:55 am
by Mannsean
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

Re: Built in function library

Posted: Sat Sep 12, 2020 11:47 pm
by pe1chl
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.

Re: Built in function library

Posted: Thu Sep 17, 2020 1:27 am
by Chupaka
Even kind of ":foreach line in=[/file xxx get-lines] do={$process $line}" should be enough, I believe...

Re: Built in function library

Posted: Thu Sep 17, 2020 11:10 am
by pe1chl
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).

Re: Built in function library

Posted: Thu Sep 17, 2020 12:03 pm
by msatter
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.

Re: Built in function library

Posted: Thu Sep 17, 2020 12:27 pm
by pe1chl
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)

Re: Built in function library

Posted: Thu Sep 17, 2020 12:48 pm
by msatter
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)

Re: Built in function library

Posted: Thu Sep 17, 2020 1:56 pm
by pe1chl
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)

Re: Built in function library

Posted: Thu Sep 17, 2020 3:31 pm
by Chupaka
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...

Re: Built in function library

Posted: Thu Sep 17, 2020 4:08 pm
by pe1chl
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...

Re: Built in function library

Posted: Thu Sep 17, 2020 8:18 pm
by Chupaka
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...

Re: Built in function library

Posted: Thu Sep 17, 2020 9:37 pm
by pe1chl
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...

Re: Built in function library

Posted: Sun Oct 25, 2020 8:40 pm
by SimonThomasen
read+Write 1 line of text, append to text file, to not have to read/write the whole file

Re: Built in function library

Posted: Tue Oct 27, 2020 6:43 pm
by Larsa
Catch error messages

Re: Built in function library

Posted: Tue Oct 27, 2020 6:45 pm
by Larsa
micro python :)

Re: Built in function library

Posted: Tue Oct 27, 2020 9:54 pm
by Chupaka
Catch error messages
You mean, in here? https://wiki.mikrotik.com/wiki/Manual:S ... ime_errors

Re: Built in function library

Posted: Tue Oct 27, 2020 11:16 pm
by Larsa
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.

Re: Built in function library

Posted: Sun Aug 22, 2021 4:35 am
by doof
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.

Re: Built in function library

Posted: Sun Aug 22, 2021 12:09 pm
by pe1chl
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.

Re: Built in function library

Posted: Sat Oct 02, 2021 3:02 pm
by merlinthemagic7
While we wait for the builtin functions:

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

Can manipulate strings, files, md5 hashing, create GUIDs etc.

Re: Built in function library

Posted: Sat Oct 02, 2021 3:08 pm
by SiB
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.

Re: Built in function library

Posted: Sat Oct 02, 2021 3:16 pm
by rextended
@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.

Re: Built in function library

Posted: Sat Oct 02, 2021 3:34 pm
by merlinthemagic7
.....
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....

Re: Built in function library

Posted: Sat Oct 02, 2021 3:52 pm
by rextended
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.

Re: Built in function library

Posted: Sat Oct 02, 2021 4:31 pm
by SiB
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.

Re: Built in function library

Posted: Sat Oct 02, 2021 8:32 pm
by Larsa
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!

Re: Built in function library

Posted: Mon Nov 29, 2021 10:19 pm
by Amm0
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.

Re: Built in function library

Posted: Fri Dec 10, 2021 2:45 pm
by karolkaz
I'm having trouble locating the library. Is it up yet?

Re: Built in function library

Posted: Fri Dec 10, 2021 10:20 pm
by pe1chl
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.

Re: Built in function library

Posted: Mon Dec 13, 2021 8:20 am
by karolkaz
Oh, thanks for the answer!

Re: Built in function library

Posted: Mon Jan 03, 2022 2:51 pm
by max232
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.

Re: Built in function library

Posted: Mon Jan 03, 2022 11:01 pm
by Chupaka
Just wondering: where do you need this type of division?

Re: Built in function library

Posted: Wed Jan 05, 2022 12:23 pm
by mrz
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.

Re: Built in function library

Posted: Wed Jan 05, 2022 8:57 pm
by xenuc
JSON encode and parse please!

Re: Built in function library

Posted: Thu Jan 20, 2022 9:39 am
by Frozer
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 ...

Re: Built in function library

Posted: Thu Jan 20, 2022 5:56 pm
by tomaskir
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"
};

Re: Built in function library

Posted: Thu Jan 20, 2022 6:43 pm
by msatter
Replace and remove in string: viewtopic.php?p=798672#p798672

Re: Built in function library

Posted: Thu Jan 20, 2022 10:20 pm
by Amm0
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 ;).

Re: Built in function library

Posted: Fri Jan 21, 2022 12:39 am
by tomaskir
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.

Re: Built in function library

Posted: Fri Jan 21, 2022 12:47 am
by pe1chl
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).

Re: Built in function library

Posted: Fri Jan 21, 2022 1:48 am
by Amm0
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.

Re: Built in function library

Posted: Fri Jan 21, 2022 10:33 am
by pe1chl
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.

Re: Built in function library

Posted: Fri Jan 21, 2022 9:07 pm
by Chupaka
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

Re: Built in function library

Posted: Thu Jan 27, 2022 6:31 am
by jr0dd
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:

Re: Built in function library

Posted: Tue Feb 15, 2022 12:49 am
by Amm0
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.

Re: Built in function library

Posted: Mon Aug 15, 2022 11:12 am
by giulianoz
I too would love to see curl/jq as built-ins :)

Re: Built in function library

Posted: Sun Jan 15, 2023 5:43 pm
by mada3k
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"

Re: Built in function library

Posted: Mon Jul 17, 2023 4:01 pm
by Sertik
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 !

Re: Built in function library

Posted: Mon Jul 17, 2023 4:13 pm
by Sertik
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.

Re: Built in function library

Posted: Mon Jul 24, 2023 11:19 am
by mike548141
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

Re: Built in function library

Posted: Mon Jul 24, 2023 12:20 pm
by msatter
@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 " }

Re: Built in function library

Posted: Mon Jul 24, 2023 6:17 pm
by Amm0
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").

Re: Built in function library

Posted: Tue Jul 25, 2023 12:58 pm
by mike548141
@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"

Re: Built in function library

Posted: Tue Jul 25, 2023 1:06 pm
by mike548141
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

Re: Built in function library

Posted: Tue Jul 25, 2023 1:50 pm
by mike548141
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!

Re: Built in function library

Posted: Tue Jul 25, 2023 3:16 pm
by rextended
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???????????????

Re: Built in function library

Posted: Tue Jul 25, 2023 3:18 pm
by Amm0
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"
}

Re: Built in function library

Posted: Tue Jul 25, 2023 7:53 pm
by msatter
@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

Re: Built in function library

Posted: Tue Jul 25, 2023 10:54 pm
by rextended
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"
}

Re: Built in function library

Posted: Tue Jul 25, 2023 11:00 pm
by rextended
@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"

Re: Built in function library

Posted: Wed Aug 02, 2023 7:14 pm
by patrickmkt
I would like to get an AES encryption function. Or an access to openssl functions.

Re: Built in function library

Posted: Wed Dec 27, 2023 5:31 pm
by PackElend
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
...	

Re: Built in function library

Posted: Wed Dec 27, 2023 5:54 pm
by rextended
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]...

Re: Built in function library

Posted: Sun Mar 03, 2024 11:19 am
by MakroTok
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.

Re: Built in function library

Posted: Sun Mar 03, 2024 2:27 pm
by MakroTok
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.

Re: Built in function library

Posted: Fri Mar 29, 2024 1:57 pm
by ohilton576
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.

Re: Built in function library

Posted: Sat Mar 30, 2024 12:37 pm
by merlinthemagic7
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

Re: Built in function library

Posted: Sat Mar 30, 2024 6:10 pm
by Amm0
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).

Re: Built in function library

Posted: Mon Apr 01, 2024 9:11 am
by merlinthemagic7
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.

Re: Built in function library

Posted: Mon Apr 01, 2024 11:57 am
by pe1chl
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).

Re: Built in function library

Posted: Mon Apr 01, 2024 10:26 pm
by PackElend
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?

Re: Built in function library

Posted: Tue Apr 02, 2024 9:07 am
by merlinthemagic7
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

Re: Built in function library

Posted: Wed Apr 03, 2024 1:51 pm
by PackElend
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.

Re: Built in function library

Posted: Thu Apr 04, 2024 2:27 pm
by merlinthemagic7
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 

Re: Built in function library

Posted: Thu Apr 04, 2024 3:57 pm
by PackElend
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