Advertisement
sa_drug

[MT]: Scripts - sys-ros-m9e

Nov 21st, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.56 KB | None | 0 0
  1. /system script add comment="=SYS-ROS-M9E=" dont-require-permissions=no name=sys-ros-m9e owner=admin policy=ftp,reboot,read,write,policy,test,sniff source="# System Maintenance [ ver. 2.3.5 ]\
  2.    \n# 1. Upgrade ROS and Bootloader;\
  3.    \n# Require: ROS 6.42 or higher\
  4.    \n# Links:\
  5.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting\
  6.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Tricks\
  7.    \n# Shevchenko Dritry - 29.08.2019 GMT+4 2019\
  8.    \n\
  9.    \n# 1. Global Variables\
  10.    \n# 1.1 Allow ROS update \
  11.    \n:global m9eUpdateROS;\
  12.    \n:if ( [ :len \$m9eUpdateROS ] = 0 ) do={ :set m9eUpdateROS false }\
  13.    \n# 1.2 List of functions\
  14.    \n# 1.2.1 Loading functions\
  15.    \n:do { /system script run ros-functions } on-error={ \
  16.    \n\t:log error ( \"sys-ros-m9e: can't load necessery functions\" );\
  17.    \n\t:error \"[ERR]: can't load necessery functions - inconsistent list of scrips\" ;\
  18.    \n}\
  19.    \n# 1.2.2 Fuctions declaration\
  20.    \n:global funcIsFlash;\
  21.    \n:global funcCmd2Script;\
  22.    \n:global funcROSVersionCompare;\
  23.    \n# 2. Local Variables\
  24.    \n# 2.1 ROS firmware versions\
  25.    \n:local roscurrentfmw [ /system resource get version ];\
  26.    \n:if ( \$roscurrentfmw ~\"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+.*\\\\(.*\\\\)\\\$\" ) do={\
  27.    \n\t:set roscurrentfmw [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \" \" ] ];\
  28.    \n}\
  29.    \n:local rosupgradefmw;\
  30.    \n:local roscurmajver [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \".\" ] ];\
  31.    \n# 2.2 ROS upgrade needed\
  32.    \n:local rosupgrade false;\
  33.    \n# 2.3 Restart needed\
  34.    \n:local rbtFile \"ros-upgrade-once.txt\";\
  35.    \n:if ( [ :len [ file find where type=\"disk\" and name=\"flash\" ] ] != 0 ) do={ :set rbtFile \"flash/ros-upgrade-once.txt\" }\
  36.    \n# 2.4 Verbose output\
  37.    \n:local VERBOSE false;\
  38.    \n\
  39.    \n# 3. Main\
  40.    \n# 3.0 Version check\
  41.    \n:if ( [ \$funcROSVersionCompare versionA=\$roscurrentfmw versionB=\"6.42\" ] < 0 ) do={\
  42.    \n\t:log error (\"sys-ros-m9e: ros version is less than minimal required\");\
  43.    \n\t:if ( \$VERBOSE ) do={ :error \"[ERR]: ROS version is less than minimal required.\" }\
  44.    \n}\
  45.    \n# 3.1 Bootloader firmware auto-upgrade\
  46.    \n:local rbdcurrentupd [ /system routerboard settings get auto-upgrade ];\
  47.    \n\
  48.    \n# 3.2 Upgrade proccess\
  49.    \n# 3.2.1 Check if new firmware availabale\
  50.    \n/system package update check-for-updates once;\
  51.    \n:delay 10s; \
  52.    \n:set rosupgradefmw [ /system package update get latest-version ];\
  53.    \n\
  54.    \n:if ( \$roscurrentfmw != \$rosupgradefmw ) do={\
  55.    \n\t:if ( \$m9eUpdateROS ) do= { \
  56.    \n\t\t:if ( \$VERBOSE ) do={ :put \"WRN: Upgrading RouterOS firmware\" }\
  57.    \n\t\t:set rosupgrade true;\
  58.    \n\t\t:set m9eUpdateROS false;\
  59.    \n\t} else={\
  60.    \n\t\t:if ( \$VERBOSE ) do={ :put \"WRN: RouterOS upgrades avaliable but disabled\" }\
  61.    \n\t\tlog info ( \"sys-ros-m9e: new ros firmware avaliable but upgrade disabled\" );\
  62.    \n\t}\
  63.    \n} else={\
  64.    \n\t:if ( \$VERBOSE ) do={ :put \"[MSG]: RouterOS firmware is up to date\" }\
  65.    \n\tlog info ( \"sys-ros-m9e: ros firmware is up to date\" );\
  66.    \n}\
  67.    \n# 3.2.2 Upgrade process\
  68.    \n:if ( \$rosupgrade ) do={\
  69.    \n\t:do {\
  70.    \n\t\tlog warning ( \"sys-ros-m9e: upgrading ros firmware from \$roscurrentfmw to \$rosupgradefmw\" );\
  71.    \n\t\tlog warning ( \"sys-ros-m9e: system will be rebooted\" );\
  72.    \n\t\t:if ( \$rbdcurrentupd ) do={\
  73.    \n\t\t\t:local command ( \"/file print file=\" . \$rbtFile );\
  74.    \n\t\t\t\$funcCmd2Script cmd=\$command\
  75.    \n\t\t} else={\
  76.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"WRN: You should enable routerboard firmware auto-upgrade\" }\
  77.    \n\t\t\tlog warn ( \"sys-ros-m9e: you should enable rbd firmware auto-upgrade\" )\
  78.    \n\t\t}\
  79.    \n\t\t/system package update install;\
  80.    \n\t\tdelay 240s;\
  81.    \n\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: RouterOS firmware upgraded\" }\
  82.    \n\t} on-error={\
  83.    \n\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: RouterOS firmware upgrade failed!\" }\
  84.    \n\t\tlog error ( \"sys-ros-m9e: ros firmware upgrade failed, reboot system\" )\
  85.    \n\t\t/system reboot;\
  86.    \n\t}\
  87.    \n};\
  88.    \n# 3.3 Environment cleanup\
  89.    \n:do { /system script environment remove [ find name~\"func\" ] } on-error={\
  90.    \n\t:log warning ( \"sys-ros-m9e: script environment cleanup is failed\" );\
  91.    \n};\
  92.    \n# 4. End"
  93.  
  94. /system scheduler add comment="System mainteinance" interval=1d name=sys-m9e on-event=sys-ros-m9e policy=ftp,reboot,read,write,policy,test,sniff start-date=sep/10/2019 start-time=00:30:00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement