aboutsummaryrefslogtreecommitdiffstats
path: root/check-routeros-update
blob: f0b06a16bb830a376e02e1e10aef20ac627b38ff (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!rsc
# RouterOS script: check-routeros-update
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
#
# check for RouterOS update, send notification e-mails

:global Identity;
:global SafeUpdateUrl;
:global SentRouterosUpdateNotification;

:global SendNotification;

:if ([ / system package print count-only where name="wireless" disabled=no ] > 0) do={
  :if ([ / interface wireless cap get enabled ] = true && \
      [ / caps-man manager get enabled ] = false) do={
    :error "System is managed by CAPsMAN, not checking.";
  }
}

/ system package update check-for-updates without-paging;
:local InstalledVersion [ / system package update get installed-version ];
:local LatestVersion [ / system package update get latest-version ];

:if ($InstalledVersion != $LatestVersion) do={
  :local Channel [ / system package update get channel ];
  :local Model [ / system routerboard get model ];
  :local SerialNumber [ / system routerboard get serial-number ];

  :if ([ :len $SafeUpdateUrl ] > 0) do={
    :local Result;
    :do {
      :set Result [ / tool fetch check-certificate=yes-without-crl \
          ($SafeUpdateUrl . $Channel . "?installed=" . $InstalledVersion . \
          "&latest=" . $LatestVersion) output=user as-value ];
    } on-error={
      :log warning ("Failed receiving safe version for " . $Channel . ".");
    }
    :if ($Result->"status" = "finished" && $Result->"data" = $LatestVersion) do={
      :log info ("Version " . $LatestVersion . " is considered safe, updating...");
      $SendNotification ("RouterOS update notification") \
          ("Version " . $LatestVersion . " is considered safe for " . $Channel . \
          ", updating on " . $Identity . "...");
      :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.";
    }
  }

  :if ($SentRouterosUpdateNotification = $LatestVersion) do={
    :error ("Already sent the RouterOS update notification for version " . \
        $LatestVersion . ".");
  }

  $SendNotification ("RouterOS update notification") \
    ("There is a RouterOS update available\n\n" . \
      "Routerboard:   " . $Model . "\n" . \
      "Serial number: " . $SerialNumber . "\n" . \
      "Hostname:      " . $Identity . "\n" . \
      "Channel:       " . $Channel . "\n" . \
      "Installed:     " . $InstalledVersion . "\n" . \
      "Available:     " . $LatestVersion);
  :set SentRouterosUpdateNotification $LatestVersion;
}