2018-09-26 22:18:43 +00:00
|
|
|
#!rsc
|
2018-07-05 13:29:26 +00:00
|
|
|
# RouterOS script: check-routeros-update
|
2020-01-01 16:00:39 +00:00
|
|
|
# Copyright (c) 2013-2020 Christian Hesse <mail@eworm.de>
|
2018-07-05 13:29:26 +00:00
|
|
|
#
|
2019-05-23 10:16:34 +00:00
|
|
|
# check for RouterOS update, send notification and/or install
|
2020-03-27 20:42:19 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-routeros-update.md
|
2018-07-05 13:29:26 +00:00
|
|
|
|
global: variable names are CamelCase
___ _ ___ __
/ _ )(_)__ _ / _/__ _/ /_
/ _ / / _ `/ / _/ _ `/ __/
/____/_/\_, / /_/ \_,_/\__/
_ __ /___/ _ __
| | / /___ __________ (_)___ ____ _/ /
| | /| / / __ `/ ___/ __ \/ / __ \/ __ `/ /
| |/ |/ / /_/ / / / / / / / / / / /_/ /_/
|__/|__/\__,_/_/ /_/ /_/_/_/ /_/\__, (_)
/____/
RouterOS has some odd behavior when it comes to variable names. Let's
have a look at the interfaces:
[admin@MikroTik] > / interface print where name=en1
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
That looks ok. Now we use a script:
{ :local interface "en1";
/ interface print where name=$interface; }
And the result...
[admin@MikroTik] > { :local interface "en1";
{... / interface print where name=$interface; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
... still looks ok.
We make a little modification to the script:
{ :local name "en1";
/ interface print where name=$name; }
And the result:
[admin@MikroTik] > { :local name "en1";
{... / interface print where name=$name; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
1 S en2 ether 1500 1598
2 S en3 ether 1500 1598
3 S en4 ether 1500 1598
4 S en5 ether 1500 1598
5 R br-local bridge 1500 1598
Ups! The filter has no effect!
That happens whenever the variable name ($name) matches the property
name (name=).
And another modification:
{ :local type "en1";
/ interface print where name=$type; }
And the result:
[admin@MikroTik] > { :local type "en1";
{... / interface print where name=$type; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
Ups! Nothing?
Even if the variable name ($type) matches whatever property name (type=)
things go wrong.
The answer from MikroTik support (in Ticket#2019010222000454):
> This is how scripting works in RouterOS and we will not fix it.
To get around this we use variable names in CamelCase. Let's hope
Mikrotik never ever introduces property names in CamelCase...
*fingers crossed*
2019-01-03 16:45:43 +00:00
|
|
|
:global Identity;
|
|
|
|
:global SafeUpdateUrl;
|
|
|
|
:global SentRouterosUpdateNotification;
|
2018-07-05 13:29:26 +00:00
|
|
|
|
2019-09-12 11:45:44 +00:00
|
|
|
:global DeviceInfo;
|
2020-02-28 14:26:26 +00:00
|
|
|
:global LogPrintExit;
|
2019-12-03 21:07:29 +00:00
|
|
|
:global ScriptFromTerminal;
|
2018-10-09 13:53:27 +00:00
|
|
|
:global SendNotification;
|
|
|
|
|
2019-07-24 08:59:53 +00:00
|
|
|
:local DoUpdate do={
|
2019-02-21 17:03:51 +00:00
|
|
|
:if ([ / system script print count-only where name="packages-update" ] > 0) do={
|
|
|
|
/ system script run packages-update;
|
|
|
|
} else={
|
|
|
|
/ system package update install without-paging;
|
|
|
|
}
|
|
|
|
:error "Waiting for system to reboot.";
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:37:19 +00:00
|
|
|
:if ([ / system package print count-only where name="wireless" disabled=no ] > 0) do={
|
2018-07-09 18:41:31 +00:00
|
|
|
:if ([ / interface wireless cap get enabled ] = true && \
|
|
|
|
[ / caps-man manager get enabled ] = false) do={
|
2020-02-26 13:19:54 +00:00
|
|
|
$LogPrintExit error "System is managed by CAPsMAN, not checking." true;
|
2018-07-05 13:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:11:50 +00:00
|
|
|
:if ([ / system scheduler print count-only where name="reboot-for-update" ] > 0) do={
|
|
|
|
:error "A reboot for update is already scheduled.";
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:29:26 +00:00
|
|
|
/ system package update check-for-updates without-paging;
|
2019-07-24 08:59:53 +00:00
|
|
|
:local Update [ / system package update get ];
|
2018-08-30 06:18:17 +00:00
|
|
|
|
2019-08-08 18:27:11 +00:00
|
|
|
:if ([ :len ($Update->"latest-version") ] = 0) do={
|
2020-02-26 13:19:54 +00:00
|
|
|
$LogPrintExit warning "An empty string is not a valid version." true;
|
2019-08-08 18:27:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:59:53 +00:00
|
|
|
:if ($Update->"installed-version" != $Update->"latest-version") do={
|
global: variable names are CamelCase
___ _ ___ __
/ _ )(_)__ _ / _/__ _/ /_
/ _ / / _ `/ / _/ _ `/ __/
/____/_/\_, / /_/ \_,_/\__/
_ __ /___/ _ __
| | / /___ __________ (_)___ ____ _/ /
| | /| / / __ `/ ___/ __ \/ / __ \/ __ `/ /
| |/ |/ / /_/ / / / / / / / / / / /_/ /_/
|__/|__/\__,_/_/ /_/ /_/_/_/ /_/\__, (_)
/____/
RouterOS has some odd behavior when it comes to variable names. Let's
have a look at the interfaces:
[admin@MikroTik] > / interface print where name=en1
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
That looks ok. Now we use a script:
{ :local interface "en1";
/ interface print where name=$interface; }
And the result...
[admin@MikroTik] > { :local interface "en1";
{... / interface print where name=$interface; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
... still looks ok.
We make a little modification to the script:
{ :local name "en1";
/ interface print where name=$name; }
And the result:
[admin@MikroTik] > { :local name "en1";
{... / interface print where name=$name; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
1 S en2 ether 1500 1598
2 S en3 ether 1500 1598
3 S en4 ether 1500 1598
4 S en5 ether 1500 1598
5 R br-local bridge 1500 1598
Ups! The filter has no effect!
That happens whenever the variable name ($name) matches the property
name (name=).
And another modification:
{ :local type "en1";
/ interface print where name=$type; }
And the result:
[admin@MikroTik] > { :local type "en1";
{... / interface print where name=$type; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
Ups! Nothing?
Even if the variable name ($type) matches whatever property name (type=)
things go wrong.
The answer from MikroTik support (in Ticket#2019010222000454):
> This is how scripting works in RouterOS and we will not fix it.
To get around this we use variable names in CamelCase. Let's hope
Mikrotik never ever introduces property names in CamelCase...
*fingers crossed*
2019-01-03 16:45:43 +00:00
|
|
|
:if ([ :len $SafeUpdateUrl ] > 0) do={
|
|
|
|
:local Result;
|
2019-01-02 09:45:20 +00:00
|
|
|
:do {
|
global: variable names are CamelCase
___ _ ___ __
/ _ )(_)__ _ / _/__ _/ /_
/ _ / / _ `/ / _/ _ `/ __/
/____/_/\_, / /_/ \_,_/\__/
_ __ /___/ _ __
| | / /___ __________ (_)___ ____ _/ /
| | /| / / __ `/ ___/ __ \/ / __ \/ __ `/ /
| |/ |/ / /_/ / / / / / / / / / / /_/ /_/
|__/|__/\__,_/_/ /_/ /_/_/_/ /_/\__, (_)
/____/
RouterOS has some odd behavior when it comes to variable names. Let's
have a look at the interfaces:
[admin@MikroTik] > / interface print where name=en1
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
That looks ok. Now we use a script:
{ :local interface "en1";
/ interface print where name=$interface; }
And the result...
[admin@MikroTik] > { :local interface "en1";
{... / interface print where name=$interface; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
... still looks ok.
We make a little modification to the script:
{ :local name "en1";
/ interface print where name=$name; }
And the result:
[admin@MikroTik] > { :local name "en1";
{... / interface print where name=$name; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
1 S en2 ether 1500 1598
2 S en3 ether 1500 1598
3 S en4 ether 1500 1598
4 S en5 ether 1500 1598
5 R br-local bridge 1500 1598
Ups! The filter has no effect!
That happens whenever the variable name ($name) matches the property
name (name=).
And another modification:
{ :local type "en1";
/ interface print where name=$type; }
And the result:
[admin@MikroTik] > { :local type "en1";
{... / interface print where name=$type; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
Ups! Nothing?
Even if the variable name ($type) matches whatever property name (type=)
things go wrong.
The answer from MikroTik support (in Ticket#2019010222000454):
> This is how scripting works in RouterOS and we will not fix it.
To get around this we use variable names in CamelCase. Let's hope
Mikrotik never ever introduces property names in CamelCase...
*fingers crossed*
2019-01-03 16:45:43 +00:00
|
|
|
:set Result [ / tool fetch check-certificate=yes-without-crl \
|
2019-07-24 08:59:53 +00:00
|
|
|
($SafeUpdateUrl . $Update->"channel" . "?installed=" . $Update->"installed-version" . \
|
|
|
|
"&latest=" . $Update->"latest-version") output=user as-value ];
|
2019-01-02 09:45:20 +00:00
|
|
|
} on-error={
|
2020-03-05 08:08:57 +00:00
|
|
|
$LogPrintExit warning ("Failed receiving safe version for " . $Update->"channel" . ".") false;
|
2019-01-02 09:45:20 +00:00
|
|
|
}
|
2019-07-24 08:59:53 +00:00
|
|
|
:if ($Result->"status" = "finished" && $Result->"data" = $Update->"latest-version") do={
|
2020-03-05 08:08:57 +00:00
|
|
|
$LogPrintExit info ("Version " . $Update->"latest-version" . " is considered safe, updating...") false;
|
2019-05-23 10:16:34 +00:00
|
|
|
$SendNotification ("RouterOS update") \
|
2019-07-24 08:59:53 +00:00
|
|
|
("Version " . $Update->"latest-version" . " is considered safe for " . $Update->"channel" . \
|
2019-08-18 17:16:08 +00:00
|
|
|
", updating on " . $Identity . "...") "" "true";
|
2019-07-24 08:59:53 +00:00
|
|
|
$DoUpdate;
|
2019-02-21 17:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 21:07:29 +00:00
|
|
|
:if ([ $ScriptFromTerminal "check-routeros-update" ] = true) do={
|
2019-07-24 08:59:53 +00:00
|
|
|
:put ("Do you want to install RouterOS version " . $Update->"latest-version" . "? [y/N]");
|
2019-02-21 17:03:51 +00:00
|
|
|
:if ([ :terminal inkey timeout=60 ] = 121) do={
|
2019-07-24 08:59:53 +00:00
|
|
|
$DoUpdate;
|
2019-02-21 17:03:51 +00:00
|
|
|
} else={
|
|
|
|
:put "Canceled...";
|
2018-08-30 09:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 08:59:53 +00:00
|
|
|
:if ($SentRouterosUpdateNotification = $Update->"latest-version") do={
|
2020-03-05 08:08:57 +00:00
|
|
|
$LogPrintExit info ("Already sent the RouterOS update notification for version " . \
|
|
|
|
$Update->"latest-version" . ".") true;
|
2018-08-30 09:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:35:40 +00:00
|
|
|
$SendNotification ("RouterOS update \E2\9C\A8") \
|
2019-05-23 10:16:34 +00:00
|
|
|
("There is a RouterOS update available.\n\n" . \
|
2019-09-12 11:45:44 +00:00
|
|
|
[ $DeviceInfo ] . "\n\n" . \
|
2019-08-18 17:16:08 +00:00
|
|
|
"https://mikrotik.com/download/changelogs/" . $Update->"channel" . "-release-tree") \
|
|
|
|
"" "true";
|
2019-07-24 08:59:53 +00:00
|
|
|
:set SentRouterosUpdateNotification ($Update->"latest-version");
|
2018-07-05 13:29:26 +00:00
|
|
|
}
|