From 9546cd650598de398878e1f91fe4788b69ae743a Mon Sep 17 00:00:00 2001 From: Aleksandr Tebiev Date: Tue, 7 Aug 2018 16:31:35 +0300 Subject: [PATCH] Initial commit --- Auto-Upgrade/README.md | 4 ++ Auto-Upgrade/firmware-updater.rsc | 109 ++++++++++++++++++++++++++++++ LICENSE | 21 ++++++ README.md | 2 + 4 files changed, 136 insertions(+) create mode 100644 Auto-Upgrade/README.md create mode 100644 Auto-Upgrade/firmware-updater.rsc create mode 100644 LICENSE create mode 100644 README.md diff --git a/Auto-Upgrade/README.md b/Auto-Upgrade/README.md new file mode 100644 index 0000000..05f6fa9 --- /dev/null +++ b/Auto-Upgrade/README.md @@ -0,0 +1,4 @@ +# MikrtoTik RouterOS automatic firmware update + +## firmware-updater.rsc + diff --git a/Auto-Upgrade/firmware-updater.rsc b/Auto-Upgrade/firmware-updater.rsc new file mode 100644 index 0000000..6af37f6 --- /dev/null +++ b/Auto-Upgrade/firmware-updater.rsc @@ -0,0 +1,109 @@ +# Script name: firmware-updater + +########## Set variables +## Notification e-mail +:local emailEnabled true +:local emailAddress "email@example.com" +:local sendBackupToEmail true +########## + + +## !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE !!!! ## + +:global beeUpdateStep; + +## Check for update +/system package update +set channel=current +check-for-updates + +## Wait on slow connections +:delay 15s; + +## First step, check for new updates +:if ([:len $beeUpdateStep] = 0 && [get installed-version] != [get latest-version]) do={ + ## New version of RouterOS available, let's upgrade + :log info ("Upgrading RouterOS on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system package update get installed-version] to $[/system package update get latest-version] (channel:$[/system package update get channel])") + + :if ($emailEnabled = true) do={ + :local attachments; + + :if ($sendBackupToEmail = true) do={ + ## date and time in format: 2018aug06-215139 + :local dtame ([:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . "-" . [:pick [/system clock get time] 0 2] . [:pick [/system clock get time] 3 5] . [:pick [/system clock get time] 6 8]); + ## unified backup file name without extension + :local bname "$[/system identity get name].$[/system routerboard get serial-number].v$[/system package update get installed-version]_$dtame" + :local sysFileBackup "$bname.backup" + :local configFileBackup "$bname.rsc" + :set attachments {$sysFileBackup;$configFileBackup} + + ## Make system backup + /system backup save dont-encrypt=yes name=$bname + ## Export config file + /export compact file=$bname + + ## Wait until bakup is done + :delay 15s; + } + + /tool e-mail send to=$emailAddress subject="Upgrade router: $[/system identity get name] FW has been started" body="Upgrading RouterOS on router $[/system identity get name] from $[/system package update get installed-version] to $[/system package update get latest-version] \r\nYou will recieve final report with detailed information when upgrade process is finished. If you have not got second email in next 5 minutes, then probably something went wrong." file=$attachments + + ## Wait for mail to be send & upgrade + :delay 15s; + + ## Remove backups which we have already sent + :if ($sendBackupToEmail = true && [/tool e-mail get last-status] = "succeeded") do={ + /file remove $attachments; + } + } + + + ## Set scheduled task to upgrade routerboard firmware on the next boot, task will be deleted when upgrade is done. (That is why you should keep original script name) + /system schedule add name=BEE-UPGRADE-NEXT-BOOT on-event=":global beeUpdateStep \"routerboardUpgrade\"; :delay 1s; /system script run firmware-updater;" start-time=startup interval=0 + + ## "install" command is reincarnation of the "upgrade" command - doing exactly the same but under a different name + install +} + +## Second step (after first reboot) routerboard firmware upgrade +:if ( $beeUpdateStep = "routerboardUpgrade") do={ + + ## Remove global variable for this step + /system script environment remove beeUpdateStep; + ## Remove task because we need it just once, right after reboot next to RouterOS install + /system scheduler remove BEE-UPGRADE-NEXT-BOOT + + ## RouterOS latest, let's check for updated firmware + /system routerboard + + :if ( [get current-firmware] != [get upgrade-firmware]) do={ + ## New version of firmware available, let's upgrade + :log info ("Upgrading firmware on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system routerboard get current-firmware] to $[/system routerboard get upgrade-firmware]") + + ## Start the upgrading process + upgrade + + ## Wait until the upgrade is finished + :delay 60s; + + ## Set scheduled task to send final report on the next boot, task will be deleted when is is done. (That is why you should keep original script name) + /system schedule add name=BEE-FINAL-REPORT-NEXT-BOOT on-event=":global beeUpdateStep \"finalReport\"; :delay 1s; /system script run firmware-updater;" start-time=startup interval=0 + + ## Reboot system to boot with new firmware + /system reboot + } +} + +## Last step (after second reboot) sending final report +:if ( $beeUpdateStep = "finalReport") do={ + ## Remove global variable for this step + /system script environment remove beeUpdateStep; + ## Remove task because we need it just once + /system scheduler remove BEE-FINAL-REPORT-NEXT-BOOT + + :log info "Upgrading RouterOS and routerboard firmware finished. Current RouterOS version: $[/system package update get installed-version], routerboard firmware: $[/system routerboard get current-firmware]." + + :if ($emailEnabled = true) do={ + /tool e-mail send to="$emailAddress" subject="Router: $[/system identity get name] has been upgraded with new FW!" body="Upgrading RouterOS and routerboard firmware finished. \r\n\r\nRouter name: $[/system identity get name]\r\nCurrent RouterOS version: $[/system package update get installed-version]; Routerboard firmware: $[/system routerboard get current-firmware]; Update channel: $[/system package update get channel]; \r\nBoard name: $[/system resource get board-name]; Serial number: $[/system routerboard get serial-number]; \r\n\r\n Changelog: https://mikrotik.com/download/changelogs/current-release-tree" + } +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b6f92df --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Aleksandr Tebiev + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b51f7c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# mikrotik +Useful scripts for MikroTik RouterOS