Community discussions

MikroTik App
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 18968
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Multiple Scripts or Multiple Functions In a Script.

Mon Feb 19, 2018 1:07 am

Hmm, first post nowhere to be found. Wondering if there is a review period?

Newbie here just got my microtik and have very basic knowledge.
Questions are conceptual vice specific details of setups.
Basically, is it better, more efficient to combine scripts or keep them separate from each other.

Background: Downloading three separate files (from same site) to combat malware etc.......

Method: Three parts:
1-Download scripts one per file (3 scripts). Each with its own schedule.
2-Transfer scripts, one per file - basically transferring them from the File Directory to the IP address list location (3 scripts). Each with its own schedule.
3-One Remove script - basically deletes all files (somewhat more efficient in approach). Also has its own schedule.

The Schedule, in practical terms looks like below with the downloads 5min apart, the transfers 1min apart, and the delete approx 5 min after the last transfer.
Download - transfer
Download - transfer
Download - transfer
Delete

By the way my first attempt following a Google search was to state /remove file [find] as the article suggested stating [find] would remove all files in the directory.
After this rule had no impact I went to /remove file [find where size>1]. That seemed to work great.

Discussion: Is it feasible to combine the above scripts and by that I mean physically possible and secondly will it save memory? Certainly it would seem like a good idea to combine at least the download and the transfer for each file type into one script and one schedule. This approach would realize 8 entities (4 scripts and 4 schedules, including the delete functionality) vice the current 14 entities (7 scripts and 7 schedules).

Even if its physically possible to combine the scripts will it it be impractical in the sense that the router may end up transferring an incomplete file or the transfer function may interrupt the download. If this is the case, is it possible to inject delays on functions or commands within scripts, such that one could avoid collisions in functionality that may interfere with processes??

Cheers
anav
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Multiple Scripts or Multiple Functions In a Script.

Mon Feb 19, 2018 4:03 pm

IMHO, it's usually best to download everything as one file BUT then import it as multiple scripts/schedules. If something is used across multiple scripts/schedules, import it as a global variable that is a function, and use that function when needed.

This is for scripts. However, by the way you're describing your use case, it sounds like what you're really importing is address lists that need to be refreshed at regular intervals, with the different lists coming from different sources. If that's the case, I'd reccomend you deal with each source separately - download the list separately, add them to different address lists, and clear said list before recreating it at the scheduled time.

Whether it's one file or more, "/tool fetch" will error on an incomplete transfer, and you can act accordingly by surrounding it inside a ":do {} on-error={}", so no need to worry about that part. To actually use the downloaded file, you only need to add a ":delay 2s" to allow the file to be fully flushed to HDD (as it sits in RAM before that).
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 18968
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Multiple Scripts or Multiple Functions In a Script.

Mon Feb 19, 2018 10:20 pm

Thanks boen,
Right now I delete the files with this command
/file remove [find where size>1].
This gets rid of all files in the directory. Seeing as I don't want to keep any files in there, I don't see the harm but although seemingly efficient, it is not very elegant/precise.
Is it possible that the OS will store files in there, and as such I could be interfering with normal OS functions???

If I decide to name the files directly to be deleted, do I need a single script, 3 scripts or would it be acceptable to do by file type
/file remove [find type=".rsc file"]

if not then? (how to type multiple arguments within find?)
/file remove [find filename1, filename2, filename3] or
/file remove [find filename1; [find filename2; [find filename3]]] or
/file remove filename1; filename2; filename3 or
/file remove filename1 filename2 filename3
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Multiple Scripts or Multiple Functions In a Script.  [SOLVED]

Mon Feb 19, 2018 11:19 pm

Use the regex operator:
/file remove [find name=~"^(filename1\\.rsc|filename2\\.rsc|folder/script\\.rsc)\$"]
To remove "filename1.rsc" and "filename2.rsc" and "folder/script.rsc".

Or you could "or" multiple conditions:
/file remove [find name="filename1.rsc" or name="filename2.rsc" or name="folder/script.rsc"]
Or alternatively, you could just freely remove exact files one by one
/file remove "filename1.rsc"
/file remove "filename2.rsc"
/file remove "folder/script.rsc"
Though keep in mind that, similarly to file creation, the files will REALLY be removed 2 seconds later. Also, that last approach will error if the file is already deleted.
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 18968
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Multiple Scripts or Multiple Functions In a Script.

Tue Feb 20, 2018 3:14 pm

/file remove [find name=~"^(filename1\\.rsc|filename2\\.rsc|folder/script\\.rsc)\$"]

Thanks used the OR version and works like a charm.
Regarding the AND version depicted above.
Can you break down the function of the syntax.

The straight vertical line |appears to be acting as a separator between the filenames, does it equal the word and? or is that embedded in the other commands?

Why the two slashes between the actual filename and the appending file type designator \\.rsc ?

I have no clue as to the purpose of the syntax between the quotations and before and after the brackets "^ ( ) \$" but its odd that they don't match??

Curious as to the function of the squiggle after the equal sign =~ in the [Find section?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Multiple Scripts or Multiple Functions In a Script.

Tue Feb 20, 2018 4:03 pm

That's a regular expression, sometimes called "regex" for short. RouterOS in particular uses POSIX Extended regular expressions. There are lots of tutorials on the internet for it - here's one. You can match a string against a regular expression by using "=~" instead of just "=".

Basically, "|" is "or" between the left and right portions, "^" matches beginning of string, "$" matches end of string, "." matches any character. Using a "\" in front of a character makes it loose its special meaning, so "\." matches a literal ".", instead of any character.

So, the "real" regular expression is really "^(filename1\.rsc|filename2\.rsc|folder/script\.rsc)$", and it says the name should "begin and end with 'filename1.rsc' or begin and end with 'filename2.rsc' or begin and end with 'folder/script.rsc'". However, within RouterOS strings, the "$" character is also special (a variable reference), and "\" is also used as an escape character, with a literal slash being written as an escape "\" in front of it. So, in order to make RouterOS see the above, you write the string as "^(filename1\\.rsc|filename2\\.rsc|folder/script\\.rsc)\$".
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 18968
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Multiple Scripts or Multiple Functions In a Script.

Fri Feb 23, 2018 9:54 pm

Much obliged. I see I will have to do some extra reading on strings and expressions. I have not used this before.
POSIX ready I am not. :-)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Multiple Scripts or Multiple Functions In a Script.

Wed Jul 27, 2022 2:53 am

Hmm, first post nowhere to be found. Wondering if there is a review period?
Welcome to the forum... :lol: :lol: :lol: :lol: :lol:

Who is online

Users browsing this forum: No registered users and 25 guests