Community discussions

MikroTik App
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Script to update RouterOS after X days of release

Sun Oct 01, 2023 11:43 pm

I've had a script to check and install updates but I added functionality to update only after the release has been out for a period of time.
I do this to avoid installing a release that has bugs and has to be released again or pulled.

I run this script daily so it can count the days from the last update as well as the fw update script in case the RouterOS came with a FW update (usually does). I run RouterOSupdate then FirmwareInstall 15min later


I call this script RouterOSupdate
#This script looks to see if there is a RouterOS update available, if so, it starts counting in variable DaysSinceRelease until the release has been out for "DaysToWait" days or more.
#the variable is also saved in a system file to survive reboots that may occur.
#This script should run daily to keep the DaysSinceRelease variable up to date.
#G. Racino 01Oct2023

#days to wait after a new release before updating this device.
:local DaysToWait 30
:global LatestVer [/system package update get latest-version]

/system package update set channel=stable
/system package update
check-for-updates once
:delay 5s;

#initialize files if they do not exist
:if ([/file find name="DaysSinceRelease.txt"] = "") do={:log info "DaysSinceRelease file does not exists, creating"; /file add name=DaysSinceRelease.txt;/file set "DaysSinceRelease.txt" contents="0";delay 2s}
:global DaysSinceRelease [/file get [/file find name=DaysSinceRelease.txt] contents];

:if ([/file find name="CountingVer.txt"] = "") do={:log info "CountingVer file does not exists, creating"; /file add name=CountingVer.txt;/file set "CountingVer.txt" contents=[/system package update get installed-version];delay 2s}
:global CountingVer [/file get [/file find name=CountingVer.txt] contents];

:local newdays $DaysSinceRelease
:set $newdays ($newdays + 1)

:log info "~~~~~~~~~~RouterOS update check:  Latest Ver=$LatestVer, Installed Ver=$CountingVer ~~~~~~~~~~~"

:if ([/system package update get status] = "New version is available") do={

   :if (([/system package update get status] = "New version is available") && ($newdays <= $DaysToWait) && ($CountingVer = $LatestVer)) do={
   :log info "~~~~~~~~~New RouterOS release is out for $newdays so NOT updating this device until release has been out for $DaysToWait days~~~~~~~~~~~~~~~~" 
   :set $DaysSinceRelease $newdays;
   /file set "DaysSinceRelease.txt" contents=$newdays
   } else={

   :if ( ([/system package update get status] = "New version is available") && ($newdays > $DaysToWait) && ($CountingVer != $LatestVer)) do={
   :log info "~~~~~~~~~RouterOS version changed, need to update on next check.  Waiting period of $DaysToWait met. Wait time was $newdays of $DaysToWait, latest version is $LatestVer, tracking version was $CountingVer"
   :set CountingVer $LatestVer
   /file set "CountingVer.txt" content=$LatestVer
   /file set "DaysSinceRelease.txt" content=0
   :set $DaysSinceRelease 0
   } else={

   :if ( ([/system package update get status] = "New version is available") && ($newdays > $DaysToWait)  && ($CountingVer = $LatestVer)) do={
   :log info "~~~~~~~~~~New RouterOS release is out for $newdays so updating this device~~~~~~~~~"; 
   :set $DaysSinceRelease 0;
   /file set "DaysSinceRelease.txt" contents="0"
   :delay 3s;
   /system package update install 
   }
   }}

} else={
:log info "~~~~~~~~~~RouterOS update check: No new updates.~~~~~~~~~~~~"
/file set "DaysSinceRelease.txt" content="0"
:set $DaysSinceRelease 0
}

and this one is the fw update I call FirmwareInstall
## script to update RouterOS firmware
## check for updated firmware
:log info ("Checking for FW upgrade...")
/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] from $[/system routerboard get current-firmware] to $[/system routerboard get upgrade-firmware]")
      :delay 3s;
      upgrade

      ## Wait for upgrade, then reboot
      :delay 8s;
      /system reboot

   } else={

   :log info ("No Router FW upgrade found")

   }

}
Last edited by texmeshtexas on Mon Oct 02, 2023 4:54 pm, edited 1 time in total.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26387
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 8:58 am

Nice idea for a scripting exercise, but just to let you know - MikroTik does not do this. There is no re-release or version pull. We always release a new version.
 
nediis
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Jul 11, 2018 10:32 am

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 10:50 am

instead of working with a file, you can use a layer7 to save a variable
/ip fire layer7-protocol add/set/get
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 12:18 pm

I've had a script to check...
It's all nonsense. And even just 30 days is ridiculous.

The only thing that is actually valid when you want to proceed with the mass update is to publish on a machine/server a file or a web page where the minimum version to which the device must be updated is written, and the script will automatically download the specific file for machine architecture, and related additional packages, and will reboot to apply the update.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 12:25 pm

instead of working with a file, you can use a layer7 to save a variable
/ip fire layer7-protocol add/set/get
But what's the point of saving a file or value in layer 7 just to know how many days ago that version was released?
For example, reading just data inside
https://upgrade.mikrotik.com/routeros/LATEST.7
You obtain "7.11.2 1693490147" and if you split at the space, you have version number and build time
1693490147 = Thu Aug 31 2023 13:55:47 GMT+0000 (GMT)
So, just compare actual time with build time...
1696238651 - 1693490147 = 2748504
2748504 / 86400 = ~31 days
 
Simonej
Frequent Visitor
Frequent Visitor
Posts: 56
Joined: Sun Aug 22, 2021 3:34 am

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 1:26 pm

I had an easy scheduled script every 30 days;
/system package update check-for-updates once
:if ([get status] = "New version is available") do={
- backup
- then "install"

instead of making things complicated and updating X days after release date, I'll be more concerned to not update to the latest version, as @rextended teached time ago, never update production devices without having tried first.
This is what I have experienced and can confirm the good practice, having a scheduled update every X day or after X days after release will not cover you against critical bugs, if you want to auto-update, better at least to find a way to download the previous version.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 4:53 pm

I understand blind updating can be a problem. I'm just trying to protect from a release that had a short life and had serious bugs that got fixed in days or weeks.
Feature changes that can break things, yes, have to test that stuff, and I do.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 5:00 pm

Nice idea for a scripting exercise, but just to let you know - MikroTik does not do this. There is no re-release or version pull. We always release a new version.
Yes, I know. my intention was to wait until a new version was out for X days before updating. Its been my observation that if there was a big problem, a new one gets released pretty quick to correct any issues.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 5:06 pm

instead of working with a file, you can use a layer7 to save a variable
But what's the point of saving a file or value in layer 7 just to know how many days ago that version was released?
For example, reading just data inside
https://upgrade.mikrotik.com/routeros/LATEST.7
You obtain "7.11.2 1693490147" and if you split at the space, you have version number and build time
1693490147 = Thu Aug 31 2023 13:55:47 GMT+0000 (GMT)
So, just compare actual time with build time...
1696238651 - 1693490147 = 2748504
2748504 / 86400 = ~31 days
That was my first attempt but I could not get the build time to return in a script call
:global LatestVer [/system package update get latest-version]
This only returns the version number, no build timestamp.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26387
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: Script to update RouterOS after X days of release

Mon Oct 02, 2023 5:50 pm

Ah now I get it. If version X is latest version and after 30 days the same version is STILL the latest version, then upgrade? It makes sense, but also this situation will not be common, as we release new versons for other reasons too, not just because of problems.

I still stand by what I said, if you are on stable channel, there is no need to worry, MikroTik is not releasing potentially broken versions in there. We first test them in less stable channels
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Wed Oct 04, 2023 8:47 pm

Ah now I get it. If version X is latest version and after 30 days the same version is STILL the latest version, then upgrade? It makes sense, but also this situation will not be common, as we release new versons for other reasons too, not just because of problems.

I still stand by what I said, if you are on stable channel, there is no need to worry, MikroTik is not releasing potentially broken versions in there. We first test them in less stable channels
Is there a command similar to
:global LatestVer [/system package update get latest-version]
that also returns the build date?
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Fri Oct 06, 2023 2:34 pm

that also returns the build date?

When executing the command:
:put [/system/package/update/ get]
channel=stable;installed-version=7.11.2;latest-version=7.11.2;status=System is already up to date
No compilation date available, I have only seen it in the changelog itself:

changelog code

What's new in 7.11.2 (2023-Aug-31 16:55):

*) dhcp - fixed DHCP server "authoritative" and "delay-threshold" settings (introduced in v7.11.1);

But I don't know how we could get that date from the text itself.

BR.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Fri Oct 06, 2023 9:40 pm

There would be a solution to get the build date:

changelog code

/system package update
:local latest [get latest-version]
:local changeLog ([/tool fetch "http://upgrade.mikrotik.com/routeros/$latest/CHANGELOG" output=user as-value] -> "data")
:local dateEx [:pick $changeLog ([:find $changeLog "("]+1) [:find $changeLog "):"]]
:put $dateEx

Output:  2023-Aug-31 16:55
Edited for simplified version
Last edited by diamuxin on Sun Oct 08, 2023 10:52 pm, edited 5 times in total.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Fri Oct 06, 2023 11:11 pm

There would be a solution to get the build date:

build date code

# Get the Changelog of the latest version of the package and save it in a text file.

:local latest [/system package update get latest-version]
:local changeLog ([/tool fetch "http://upgrade.mikrotik.com/routeros/$latest/CHANGELOG" output=user as-value] -> "data")
:local filename "changelog.txt"
/file print file=$filename; :delay 2s; /file set $filename contents=$changeLog


# Read the contents of the Changelog and get the buid date
/file 
:local changelogEx [get [find name~"^change"] contents]
:local dateEx [:pick $changelogEx ([:find $changelogEx "("]+1) [:find $changelogEx "):"]]
:put $dateEx

Output:  2023-Aug-31 16:55
that is one way to do it. Thanks.
 
S8T8
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Thu Sep 15, 2022 7:15 pm

Re: Script to update RouterOS after X days of release

Tue Oct 10, 2023 8:47 pm

Edited for simplified version
Nice! Should not be more useful to pick just the date and compare it with the current date [/system clock get date], than if the date is X days after the release, install?
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Tue Oct 10, 2023 11:29 pm

Edited for simplified version
Nice! Should not be more useful to pick just the date and compare it with the current date [/system clock get date], than if the date is X days after the release, install?
Yeah, was starting to work on that but have not worked out the :pick of only the date without time. Seems pick is a little finicky. and not sure the math will work in that format.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Wed Oct 11, 2023 5:01 pm

Trouble is the date format in the changelog is different than that in the router

"2023-Aug-31 16:55" has to be converted to "2023-08-31 16:55"

found a script from @rextended to convert to epoch to do the math but even that does not like the "Aug" format.
the datetime2epoch script is here viewtopic.php?p=1029775#p1029775
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Wed Oct 11, 2023 5:20 pm

Trouble is the date format in the changelog is different than that in the router

"2023-Aug-31 16:55" has to be converted to "2023-08-31 16:55"

found a script from @rextended to convert to epoch to do the math but even that does not like the "Aug" format.
the datetime2epoch script is here viewtopic.php?p=1029775#p1029775

Simple converter: (based on a @rextended script)

:global date2ymd do={
    :local dtime [:tostr $1]
    :local yyyy; :local MM; :local dd; :local HH
    # Date format: 2023-Aug-31 16:55
    :if ([:len $dtime] = 17) do={
        :set yyyy [:pick $dtime 0 4]; :set dd [:pick $dtime 9 11]; :set HH [:pick $dtime 12 17]
        :set MM   ([:find "xxanebarprayunulugepctovec" [:pick $dtime 6 8] -1]/2); :if ($MM < 10) do={:set MM "0$MM"}
    }
    :return "$yyyy-$MM-$dd $HH"
}

output code

[admin@MikroTik] > :put [$date2ymd "2023-Aug-31 16:55"]
2023-08-31 16:55
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Thu Oct 12, 2023 12:00 am

Ok, that is rather helpful.

I've reworked the script to use current date and the log date to determine days since release.
I'm naming it "RouterOSupdateV2"
I just put the two functions in the script but they could be in a "run at boot" script to make them more global.

#This script looks to see if there is a RouterOS update available, if so, it starts counting in variable DaysSinceRelease until the release has been out for "DaysToWait" days or more.
#the variable is also saved in a system file to survive reboots that may occur.
#This script should run daily to keep the DaysSinceRelease variable up to date.
#G. Racino 01Oct2023
#Updated 12Oct2023 to directly access release date from MT web site release log. WIth help from @rextended and @diamuxin from MT forum

#days to wait after a new release before updating this device.
:local DaysToWait 30
:local DSR

/system package update set channel=stable
/system package update
check-for-updates once
:delay 5s;
:global LatestVer [/system package update get latest-version]
:global InstVer [/system package update get installed-version]

:log info "~~~~~~~~~~RouterOS update check:  Latest Ver=$LatestVer, Installed Ver=$InstVer ~~~~~~~~~~~"

#The two below functions are used to convert the time stamps to a format that can be use in a simple subtract formula.
#They are from @rextended on MT forum
#https://forum.mikrotik.com/viewtopic.php?p=1029048#p994849
:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

#located https://forum.mikrotik.com/viewtopic.php?p=1029782&hilit=date2ymd#p1029782
:global date2ymd do={
    :local dtime [:tostr $1]
    :local yyyy; :local MM; :local dd; :local HH
    # Date format: 2023-Aug-31 16:55
    :if ([:len $dtime] = 17) do={
        :set yyyy [:pick $dtime 0 4]; :set dd [:pick $dtime 9 11]; :set HH [:pick $dtime 12 17]
        :set MM   ([:find "xxanebarprayunulugepctovec" [:pick $dtime 6 8] -1]/2); :if ($MM < 10) do={:set MM "0$MM"}
    }
    :return "$yyyy-$MM-$dd $HH"
}

#Check release date of new version to see if it is DaysToWait days old before installing
:if ([/system package update get status] = "New version is available") do={

      # Get the Changelog of the latest version of the package and save it in a text file.

      :local VerChangeLog ([/tool fetch "http://upgrade.mikrotik.com/routeros/$LatestVer/CHANGELOG" output=user as-value] -> "data")

      :local dateEx [:pick $VerChangeLog ([:find $VerChangeLog "("]+1) ([:find $VerChangeLog "):"]) ]
      :local currDate [/system clock get date]

      #:log info "dateEx=$dateEx  currDate=$currDate"
      :local EPOCHdateEx [$datetime2epoch [$date2ymd $dateEx]]
      :local EPOCHcurrDate [$datetime2epoch $currDate]

      :set DSR (([:tonum $EPOCHcurrDate]-[:tonum $EPOCHdateEx])/86400)

      :if ($DSR <= $DaysToWait) do={
      :log info "~~~~~~~~~New RouterOS release is out for $DSR days so NOT updating this device until release has been out for $DaysToWait days~~~~~~~~~~~~~~~~" 
      } else={
      :log info "~~~~~~~~~~New RouterOS release is out for $DSR days so updating this device~~~~~~~~~"; 
      :delay 3s;
      /system package update install 
      }
} else={
:log info "~~~~~~~~~~RouterOS update check: No new update available.~~~~~~~~~~~~"
}
Last edited by texmeshtexas on Mon Nov 20, 2023 6:26 pm, edited 2 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Thu Oct 12, 2023 6:22 pm

I think the script is not well designed. In this part:

:if ($DSR <= $DaysToWait) do={
    :log info "~~~~~~~~~ New RouterOS release is out for $DSR so NOT updating this device until release has been out for $DaysToWait days ~~~~~~~~~~~~~~~~" 
    :set $DaysSinceRelease $newdays;
    /file set "DaysSinceRelease.txt" contents=$newdays
} else={...}

a) The local variable $DaysSinceRelease is not declared initially and neither is $newdays.

b) Is the variable $DSR the same as $DaysSinceRelease? You have to declare it with the same name.

c) You don't create correctly the file with the content of $newdays, it should be like this:

local filename "DaysSinceRelease.txt"
/file print file="$filename"; :delay 5s; set $filename contents=$newdays

Check the script because as it is it does not work.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Fri Oct 13, 2023 12:38 am

This way, I have tested it and it works OK. I hope it helps.

The $datetime2epoch and $date2ymd functions should already be in memory.

/system package update; check-for-updates without-paging as-value; :delay 3s
:global datetime2epoch
:global date2ymd
:local LatestVer  [get latest-version]
:local Status     [get status]
:local currDate   [/system clock get date]
:local DaysToWait 30
:local DSR

:if ($Status = "New version is available") do={
# :if ($Status = "System is already up to date") do={

    :local VerChangeLog  ([/tool fetch "http://upgrade.mikrotik.com/routeros/$LatestVer/CHANGELOG" output=user as-value] -> "data")
    :local dateEx        [$date2ymd [:pick $VerChangeLog ([:find $VerChangeLog "("]+1) ([:find $VerChangeLog "):"]) ]]
    :local EPOCHdateEx   [$datetime2epoch $dateEx]
    :local EPOCHcurrDate [$datetime2epoch $currDate]
    :set DSR              ((EPOCHcurrDate - $EPOCHdateEx)/86400)

    /file
    :if ($DSR < $DaysToWait) do={
        :put "Only $DSR days of the $DaysToWait have passed"
        :local filename "DaysSinceRelease.txt"
        :local output   "$DSR/$DaysToWait days since $dateEx"
        print file=$filename; :delay 5s; set $filename contents=$output
    } else={
        :put "It's time to upgrade RouterOS, it's been $DSR of $DaysToWait days of waiting."
    }
}
Last edited by diamuxin on Sat Oct 14, 2023 11:16 am, edited 1 time in total.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Fri Oct 13, 2023 3:06 am

Some of the old /file statements should have been deleted. Not needed anymore. Just left over from V1 of the script.
Should not need any file operations anymore.

DSR was define, just not at top. I moved it.

Script updated.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Fri Oct 13, 2023 10:03 am

Why without logic check CHANGELOG instead of simply use unix epoch already present on LATEST.7 NEWESTa7???

With one "data" load you have at the same time the latest version and the build time without so many frills...

https://forum.mikrotik.com/viewtopic.php?t=200103#p1028116
https://forum.mikrotik.com/viewtopic.php?p=977174#p977174

example v7 only code

:global timetoseconds do={
    :local inTime $1
    :local wPos   [:find $inTime "w" -1]
    :local dPos   [:find $inTime "d" -1]
    :local itLen  [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}
    :local itSec  [:pick $inTime ($itLen - 2) $itLen]
    :local itMin  [:pick $inTime ($itLen - 5) ($itLen - 3)]
    :local itHou  [:pick $inTime ($itLen - 8) ($itLen - 6)]
    :local itDay  0
    :local itWee  0
    :if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }
    :if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }
    :if  ([:typeof $wPos] = "num")                                do={:set itWee [:pick $inTime 0 $wPos] }
    :local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))
    :return $totitSec
}

{
# Actually the returned data end with \n. If for some reason on future \n is correctly removed, this prevent the script fail.
:local vchannel    "stable" ; # can be: [long-term | stable | testing | development]
:local lastv7      ("$([/tool fetch url="https://upgrade.mikrotik.com//routeros/NEWESTa7.$vchannel" as-value output=user]->"data")\n")
:local lastv7ver   [:pick $lastv7 0 [:find $lastv7 "\_" -1]]
:local currver     [/system/package get [find where name="routeros"] version]
:if ($lastv7ver != $currver) do={
    :local lastv7build [:pick $lastv7 ([:find $lastv7 "\_" -1] + 1) [:find $lastv7 "\n" -1]]
    :local currtime    [$timetoseconds [:timestamp]]
    :local daysdiff    (($currtime - $lastv7build) / 86400)
    :local vdays       30
    :put "The Installed version is $currver and the Latest version $lastv7ver is old $daysdiff days"
    :if ($daysdiff > $vdays) do={
        :put "Performing update: Latest is older than $vdays days"
        # /sys pack up; check; in
    } else={
        :put "Not updated: Waiting Latest for be older than $vdays days"
    }
} else={
    :put "The Installed and Latest version are the same $currver"
}

}



If you want just shorter code, without "nice" formatted days values:

simpler code

{
# Actually the returned data end with \n. If for some reason on future \n is correctly removed, this prevent the script fail.
:local vchannel    "stable" ; # can be: [long-term | stable | testing | development]
:local lastv7      ("$([/tool fetch url="https://upgrade.mikrotik.com//routeros/NEWESTa7.$vchannel" as-value output=user]->"data")\n")
:local lastv7ver   [:pick $lastv7 0 [:find $lastv7 "\_" -1]]
:local currver     [/system/package get [find where name="routeros"] version]
:if ($lastv7ver != $currver) do={
    :local lastv7build [:totime [:pick $lastv7 ([:find $lastv7 "\_" -1] + 1) [:find $lastv7 "\n" -1]]]
    :local currtime    [:timestamp]
    :local daysdiff    ($currtime - $lastv7build)
    :local vdays       30d
    :put "The Installed version is $currver and the Latest version $lastv7ver is old $daysdiff days"
    :if ($daysdiff > $vdays) do={
        :put "Performing update: Latest is older than $vdays days"
        # /sys pack up; check; in
    } else={
        :put "Not updated: Waiting Latest for be older than $vdays days"
    }
} else={
    :put "The Installed and Latest version are the same $currver"
}

}

EDIT 1: Updated the URLS, see next posts.
EDIT 2: Added channel selection.
EDIT 3: Changed post 1st line.
Last edited by rextended on Mon Dec 25, 2023 10:52 pm, edited 5 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Fri Oct 13, 2023 12:56 pm

Much better this way. Nice!

One question: Is it only applicable to the "stable" version?
https://upgrade.mikrotik.com/routeros/LATEST.7

I have another script to control the "testing" version (I use the extraction of the build date from the changelog), do you know if it is possible to get the EPOCH date in the same way?

EDIT (I answer myself)
I have tried several options and it has only worked:

test code

[admin@MikroTik] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/LATEST.7" as-value output=user]->"data")
7.11.2 1693490147

[admin@MikroTik] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/LATEST.7rc" as-value output=user]->"data")
7.12rc1 1696484807
Last edited by diamuxin on Sat Oct 14, 2023 11:19 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Sat Oct 14, 2023 11:15 am

(see next post)
Last edited by rextended on Sat Oct 14, 2023 11:27 am, edited 2 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Sat Oct 14, 2023 11:21 am

OK, thanks.
Last edited by diamuxin on Sat Oct 14, 2023 12:00 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Sat Oct 14, 2023 11:28 am

Use httpS

MikroTik is changing the urls for the new v7.10+ versions and also for latest v6:

Long term:
https://upgrade.mikrotik.com//routeros/NEWESTa7.long-term
(at time I write: "0.00\n" no long-term version)

Stable:
https://upgrade.mikrotik.com//routeros/NEWESTa7.stable
(at time I write: "7.13 1702545848\n")

Testing:
https://upgrade.mikrotik.com//routeros/NEWESTa7.testing
(at time I write: "7.14beta3 1702985489\n")

Development:
https://upgrade.mikrotik.com//routeros/NEWESTa7.development
(at time I write: "7.14beta3 1702985489\n")

I update previous scripts for reflect the change.
Last edited by rextended on Mon Dec 25, 2023 10:57 pm, edited 4 times in total.
 
DarkNate
Forum Guru
Forum Guru
Posts: 1017
Joined: Fri Jun 26, 2020 4:37 pm

Re: Script to update RouterOS after X days of release

Sat Oct 14, 2023 3:54 pm

Rather than some "script" based in RouterOS itself, something like this calls for real network automation. Use Ansible or something similar, or custom python code that runs on a seperate box.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Sun Oct 15, 2023 10:33 pm

Hi @rextended, I have detected a small discrepancy: the EPOCH calculations performed by MikroTik are different from the calculations performed by your $datetime2epoch function, what do you think?

test code

:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}


{
:global datetime2epoch
:local vchannel    "stable" ; # can be: [long-term | stable | testing | development]
:local latestData  ("$([/tool fetch url="https://upgrade.mikrotik.com//routeros/ ... .$vchannel" as-value output=user]->"data")\n"); # 7.11.2 1693490147\n\n
:local latestVer   [:pick $latestData 0 [:find $latestData "\_" -1]]; # 7.11.2
:local latestEpoch [:pick $latestData ([:find $latestData "\_" -1] + 1) [:find $latestData "\n" -1]]; # 1693490147
:local currEpoch   [$datetime2epoch [/system/package get [find where name="routeros"] build-time]]; # 1693482947 (2023-08-31 13:55:47) 
:put $latestData
:put $latestEpoch
:put $latestVer
:put $currEpoch
:put ($latestEpoch = $currEpoch)
}
# output:
7.11.2 1693490147


1693490147
7.11.2
1693482947
false  <== It doesn't match

:shock: :?
Last edited by diamuxin on Sun Oct 15, 2023 10:39 pm, edited 1 time in total.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Sun Oct 15, 2023 10:38 pm

Makes since to me. Remember EPOCH is in seconds

:put [$unixtodatetime 1693490147] --> 31/08/2023 15:55:47
:put [$unixtodatetime 1693482947] --> 31/08/2023 13:55:47

1693490147-1693482947 is 7200 which is exactly 2 hours.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Sun Oct 15, 2023 10:43 pm

Now I understand, it's because of the time difference in my country: GMT +2 (summer time). My router is set like this. Thanks.

Possible fix?:

:local currEpoch
:if ([/system clock get gmt-offset] = 7200) do={
    # Summer time. GMT Offset +02:00
    :set currEpoch ([$datetime2epoch [/system/package get [find where name="routeros"] build-time]] +7200)
} else={
    # Winter time. GMT Offset +01:00
    :set currEpoch ([$datetime2epoch [/system/package get [find where name="routeros"] build-time]] +3600)
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Mon Oct 16, 2023 2:18 am

Hi @rextended, I have detected a small discrepancy: the EPOCH calculations performed by MikroTik are different from the calculations performed by your $datetime2epoch function, what do you think?
Simply: I have considered timezone, MirkoTik not.
(I could be wrong, but I assume if NOT written, GMT is always meant... but who cares if the build has more or less up to 14 hours difference...)
Is why my above script do not uselessly contain $datetime2epoch...


And about [/system clock get gmt-offset], the correct way to use it is how is used inside my script... (until mikrotik do not fix something...)
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Mon Oct 16, 2023 2:29 pm

And about [/system clock get gmt-offset], the correct way to use it is how is used inside my script... (until mikrotik do not fix something...)

OK, very helpful

{
:local vgmt [/system clock get gmt-offset]
:if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:put $vgmt
}
Thanks.
 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Script to update RouterOS after X days of release

Tue Nov 14, 2023 6:10 pm

Since ROS 7.12 it is much easier to get a unix time.

:local currentTime [:pick [:tonsec [:timestamp]] 0 10];
:local changelog ([/tool fetch "https://upgrade.mikrotik.com//routeros/NEWEST7.stable" as-value output=user] -> "data");
:local changelogTime [:pick $changelog ([:find $changelog " "]+1) ([:find $changelog " "]+11)];

:log info "$changelogTime";
:log info "$currentTime";

Output:
1699515906
1699977978


Hope it would be helpful
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Tue Nov 14, 2023 7:34 pm

Why all that?

Suffice just
:put [:tonum [:timestamp]]
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Thu Nov 16, 2023 11:29 pm

:put [:tonum [:timestamp]] does not work for me. Returns nothing. that is on V7.11.2
 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Script to update RouterOS after X days of release

Thu Nov 23, 2023 4:20 pm

Why all that?

Suffice just
:put [:tonum [:timestamp]]
Oops, overengineered by my stupidity, thanks for simplification.

Here is my implementation of update script
P.S. If somebody want to see code of functions and telegram script used in this script - reply and I'll show you.
:local changelog ([/tool fetch "https://upgrade.mikrotik.com//routeros/NEWEST7.stable" as-value output=user] -> "data");
:local changelogTime [:pick $changelog ([:find $changelog " "]+1) ([:find $changelog " "]+11)];
:local changelogVersion [:pick $changelog 0 [:find $changelog " "]];
:local installedBuildTime [:tonum [:totime [system package get [find where name="routeros"] build-time]]];
:local currentTime [:tonum [:timestamp]];

if ($installedBuildTime != $changelogTime) do={
 /system script run FuncDeviceStatus;
 :global FuncStatus;
 :local daysDifference (($currentTime - $changelogTime) / 86400);
 :local daysTimeout 7;
 :local daysBeforeUpdate ($daysTimeout - $daysDifference);
 :if ($daysDifference >= $daysTimeout) do={
  :log warning "Updating to ROS $changelogVersion";
  :global textSent "$[$FuncStatus]%0A\F0\9F\94\80\F0\9F\86\99 <b>Updating to ROS $changelogVersion</b>";
  /system script run SendToTelegram;
  /system package update; check-for-updates; install;
  } else={
   :log warning "Update to ROS $changelogVersion will be installed in $daysBeforeUpdate days";
   :global textSent "$[$FuncStatus]%0A\E2\8C\9B\F0\9F\86\99 <b>Update to ROS $changelogVersion will be installed in $daysBeforeUpdate days</b>";
   /system script run SendToTelegram;
  }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Thu Nov 23, 2023 7:00 pm

:put [:tonum [:timestamp]] does not work for me. Returns nothing. that is on V7.11.2
Read the posts...
Since ROS 7.12 it is much easier to get a unix time.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Thu Nov 23, 2023 8:35 pm

Here is my implementation of update script
P.S. If somebody want to see code of functions and telegram script used in this script - reply and I'll show you.
Very useful your script, thanks.

Could you share your $FuncStatus function?

A tip: It is not necessary to end each line of code with ";" unless another command follows.
Last edited by diamuxin on Thu Nov 23, 2023 8:51 pm, edited 1 time in total.
 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 11:39 am

Here is my implementation of update script
P.S. If somebody want to see code of functions and telegram script used in this script - reply and I'll show you.
Very useful your script, thanks.

Could you share your $FuncStatus function?

A tip: It is not necessary to end each line of code with ";" unless another command follows.

Here is FuncStatus code, I use it in multiple scripts to get time info from Mikrotik into messages sent to Telegram.
:global FuncStatus do={
 :local deviceDate [/system clock get date];
 :local deviceTime [/system clock get time];
 :local uptime [/system resource get uptime];
 :return ($deviceDate."  ".$deviceTime."  uptime: ".$uptime);
}
:put [$FuncStatus];

Here is Telegram output:
Image
Image

I also use it for another Telegram informer scripts:
Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 11:47 am

You can replace that with
:global FuncStatus do={
    /system clock 
    :local deviceDate [get date]
    :local deviceTime [get time]
    /system resource
    :local deviceUptime [get uptime]
    :return "$deviceDate $deviceTime uptime: $deviceUptime"
}

:put [$FuncStatus]
 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 12:16 pm

You can replace that with
:global FuncStatus do={
    /system clock 
    :local deviceDate [get date]
    :local deviceTime [get time]
    /system resource
    :local deviceUptime [get uptime]
    :return "$deviceDate $deviceTime uptime: $deviceUptime"
}

:put [$FuncStatus]
Thanks! Is there any link to read about Mikrotik scripts refactoring tips ?
 
User avatar
baragoon
Member
Member
Posts: 310
Joined: Thu Jan 05, 2017 10:38 am
Location: Kyiv, UA
Contact:

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 1:04 pm

Here is Telegram output:

I also use it for another Telegram informer scripts:
Hi, could you please share these scripts?
 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 1:48 pm

Here is Telegram output:

I also use it for another Telegram informer scripts:
Hi, could you please share these scripts?
I guess it's better to create a post with all these scripts, but ok:


TelegramElectricityNotification script - runs by scheduler every 4 hours (during blackout period every 1 hour, I am from Ukraine). I use it to monitor if there's electricity in my house (usually we have blackout schedule, so with that script I can see patterns of how long and when my house gets electricity, when there is no message on schedule - I can plan my day more easily, so I won't hurry to get back home when it is no power in my area, etc..

Image
/system script run FuncDeviceStatus
:global FuncStatus
:global textSent "$[$FuncStatus]%0A\E2\9C\85 <b>Power is still here</b>"
/system script run SendToTelegram


I use Netwatch script, to check if another Mikrotik (not connected to UPS) in my house is alive. By that i can monitor when electricity turns on/off, and disable/enable low-power mode and auxiliary wifi hotspot.

UP:
/system script run FuncDeviceStatus
:global FuncStatus
:global textSent "$[$FuncStatus]%0A\E2\9A\A1\F0\9F\94\8B <b>Electricity is back on. UPS is working</b>"
:log warning "[ELECTRICITY] Blackout-hotspot disabled. Exiting power saving mode"
/interface wireless cap set enabled=no
/system routerboard settings set cpu-frequency=auto
/system script run SendToTelegram
Image

DOWN:
/system script run FuncDeviceStatus
:global FuncStatus
:global textSent "$[$FuncStatus]%0A\E2\9D\8C\F0\9F\94\8B <b>Electricity is down. UPS is working</b>"
:log warning "[ELECTRICITY] Blackout-hotspot enabled. Entering power saving mode"
/interface wireless cap set enabled=yes
/system routerboard settings set cpu-frequency=448MHz
/system script run SendToTelegram
Image

SendToTelegram is used to send messages every script generates to my and my wife's Telegram:

:global textSent
:local telegramUsers {"yourchatidhere_1";"yourchatidhere_2"}
:local botID "yourbotidhere"
:foreach chatID in $telegramUsers do={
 /tool fetch url="https://api.telegram.org/bot$botID/sendmessage?chat_id=$chatID&text=$textSent&parse_mode=HTML" keep-result=no
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Fri Nov 24, 2023 1:57 pm

Thanks! Is there any link to read about Mikrotik scripts refactoring tips ?

Ehm... no....

Read my posts and use them as examples...
viewtopic.php?t=177551
(do not post directly on that topic, follow the links and use the other topic)
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Mon Dec 18, 2023 4:44 pm

Use httpS

MikroTik is changing the urls for the new v7.10+ versions and also for latest v6:

Long term:
https://upgrade.mikrotik.com//routeros/ ... .long-term
(at time I write: "0.0\r\n" no long-term version)

Stable:
https://upgrade.mikrotik.com//routeros/NEWEST7.stable
(at time I write: "7.11.2 1693490147\n")

Testing:
https://upgrade.mikrotik.com//routeros/NEWEST7.testing
(at time I write: "7.12rc1 1696484807\n")

Development:
https://upgrade.mikrotik.com//routeros/ ... evelopment
(at time I write: "7.12rc1 1696484807\n")

I update previous scripts for reflect the change.
Hi Rex,

Do you know if the version query addresses have changed? It seems that with the latest v7.13 it doesn't work anymore:

test code

[ ] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/NEWEST7.stable" as-value output=user]->"data")
7.12.1 1700221125

[ ] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/NEWEST7.long-term" as-value output=user]->"data")
0.00

[ ] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/NEWEST7.testing" as-value output=user]->"data")
7.12.1 1700221125

[ ] > :put ([/tool fetch url="https://upgrade.mikrotik.com/routeros/N ... evelopment" as-value output=user]->"data")
7.12.1 1700221125
Thanks.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Mon Dec 18, 2023 5:07 pm

I'm still using the script version that extracts the date from change log, not the URL. Seem to be still working just fine. At the moment. But MT could change that too.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Mon Dec 18, 2023 5:47 pm

I'm still using the script version that extracts the date from change log, not the URL. Seem to be still working just fine. At the moment. But MT could change that too.
This is the change:

[ ] > :put ([/tool fetch url="http://upgrade.mikrotik.com/routeros/NEWESTa7.stable" as-value output=user]->"data")
7.13 1702545848
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to update RouterOS after X days of release

Mon Dec 18, 2023 5:53 pm

httpS
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script to update RouterOS after X days of release

Mon Dec 18, 2023 6:00 pm

httpS
OK, thx
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script to update RouterOS after X days of release

Thu Feb 22, 2024 9:23 pm

I ran into this one today.
Rather than some "script" based in RouterOS itself, something like this calls for real network automation. Use Ansible or something similar, or custom python code that runs on a seperate box.
Or at least document/publish the change in URLs. The, now wrong, "LATEST.7rc" URLs comes from older @normis forum post. So whatever automation you used to fetch the "latest long-term" would have broken.

e.g. any tool needs a HTTPS call to get the latest version of RouterOS from somewhere & they changed the URL that did that.
 
texmeshtexas
Member Candidate
Member Candidate
Topic Author
Posts: 151
Joined: Sat Oct 11, 2008 11:17 pm

Re: Script to update RouterOS after X days of release

Mon Feb 26, 2024 5:20 pm

I still use the date stamp right in the release notes that are accessed by the update command.
Works perfectly.
#This script looks to see if there is a RouterOS update available, if so, it takes the date stamp from the release notes and calculates the nuber of 
#days since the release. Then will update the local system if the time has been "DaysToWait".
#I run this script daily at 2AM
#G. Racino 01Oct2023
#Updated 12Oct2023 to directly access release date from MT web site release log. WIth help from @rextended and @diamuxin from MT forum

#days to wait after a new release before updating this device.
:local DaysToWait 30
:local DSR

/system package update set channel=stable
/system package update
check-for-updates once
:delay 5s;
:global LatestVer [/system package update get latest-version]
:global InstVer [/system package update get installed-version]

:log info "~~~~~~~~~~RouterOS update check:  Latest Ver=$LatestVer, Installed Ver=$InstVer ~~~~~~~~~~~"

#The two below functions are used to convert the time stamps to a format that can be use in a simple subtract formula.
#They are from @rextended on MT forum
#https://forum.mikrotik.com/viewtopic.php?p=1029048#p994849
:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

#located https://forum.mikrotik.com/viewtopic.php?p=1029782&hilit=date2ymd#p1029782
:global date2ymd do={
    :local dtime [:tostr $1]
    :local yyyy; :local MM; :local dd; :local HH
    # Date format: 2023-Aug-31 16:55
    :if ([:len $dtime] = 17) do={
        :set yyyy [:pick $dtime 0 4]; :set dd [:pick $dtime 9 11]; :set HH [:pick $dtime 12 17]
        :set MM   ([:find "xxanebarprayunulugepctovec" [:pick $dtime 6 8] -1]/2); :if ($MM < 10) do={:set MM "0$MM"}
    }
    :return "$yyyy-$MM-$dd $HH"
}

#Check release date of new version to see if it is DaysToWait days old before installing
:if ([/system package update get status] = "New version is available") do={

      # Get the Changelog of the latest version of the package and save it in a text file.

      :local VerChangeLog ([/tool fetch "http://upgrade.mikrotik.com/routeros/$LatestVer/CHANGELOG" output=user as-value] -> "data")

      :local dateEx [:pick $VerChangeLog ([:find $VerChangeLog "("]+1) ([:find $VerChangeLog "):"]) ]
      :local currDate [/system clock get date]

      #:log info "dateEx=$dateEx  currDate=$currDate"
      :local EPOCHdateEx [$datetime2epoch [$date2ymd $dateEx]]
      :local EPOCHcurrDate [$datetime2epoch $currDate]

      :set DSR (([:tonum $EPOCHcurrDate]-[:tonum $EPOCHdateEx])/86400)

      :if ($DSR <= $DaysToWait) do={
      :log info "~~~~~~~~~New RouterOS release is out for $DSR days so NOT updating this device until release has been out for more than $DaysToWait days~~~~~~~~~~~~~~~~" 
      } else={
      :log info "~~~~~~~~~~New RouterOS release is out for $DSR days so updating this device~~~~~~~~~"; 
      :delay 3s;
      /system package update install 
      }
} else={
:log info "~~~~~~~~~~RouterOS update check: No new update available.~~~~~~~~~~~~"
}

Who is online

Users browsing this forum: No registered users and 10 guests