routeros-scripts/mod/scriptrunonce.rsc

51 lines
1.5 KiB
Text
Raw Normal View History

#!rsc by RouterOS
# RouterOS script: mod/scriptrunonece
2024-01-01 14:25:25 +00:00
# Copyright (c) 2020-2024 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
2023-04-04 17:24:00 +00:00
#
# download script and run it once
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/scriptrunonce.md
:global ScriptRunOnce;
# fetch and run script(s) once
:set ScriptRunOnce do={
:local Scripts [ :toarray $1 ];
:global ScriptRunOnceBaseUrl;
:global ScriptRunOnceUrlSuffix;
2024-03-08 11:45:38 +00:00
:global LogPrint;
:global ValidateSyntax;
:foreach Script in=$Scripts do={
:if (!($Script ~ "^(ftp|https?|sftp)://")) do={
:if ([ :len $ScriptRunOnceBaseUrl ] = 0) do={
2024-03-08 11:45:38 +00:00
$LogPrint warning $0 ("Script '" . $Script . "' is not an url and base url is not available.");
:return false;
}
:set Script ($ScriptRunOnceBaseUrl . $Script . ".rsc" . $ScriptRunOnceUrlSuffix);
}
:local Source;
:do {
:set Source ([ /tool/fetch check-certificate=yes-without-crl $Script output=user as-value ]->"data");
} on-error={
2024-03-08 11:45:38 +00:00
$LogPrint warning $0 ("Failed fetching script '" . $Script . "'!");
}
:if ([ :len $Source ] > 0) do={
:if ([ $ValidateSyntax $Source ] = true) do={
:do {
2024-03-08 11:45:38 +00:00
$LogPrint info $0 ("Running script '" . $Script . "' now.");
[ :parse $Source ];
} on-error={
2024-03-08 11:45:38 +00:00
$LogPrint warning $0 ("The script '" . $Script . "' failed to run!");
}
} else={
2024-03-08 11:45:38 +00:00
$LogPrint warning $0 ("The script '" . $Script . "' failed syntax validation!");
}
}
}
}