Update Mikrotik to Version XX

General discussion of anything Unimus
Post Reply
dima1002
Posts: 5
Joined: Wed Aug 20, 2025 10:12 am

Sun Oct 26, 2025 3:21 pm

I’ve adapted a RouterOS update script to automatically download and install a specific RouterOS version.
The logic seems fine, but the download part using /tool fetch doesn’t work — the file never appears in /file.

Code: Select all

:global TargetVer "7.19.4"
:global Architecture
:global PkgVer
:global PkgVer1
:global PkgVer2
:global PkgVer3
:global RbVer
:global RbVer1
:global RbVer2
:global RbVer3
:global TargetVer1
:global TargetVer2
:global TargetVer3
:global PackageURL
:global urlToFetch

# --- Detect system architecture (e.g. arm, arm64, mmips, x86) ---
:set Architecture [/system resource get architecture-name]

# --- Get current RouterOS package version ---
:set PkgVer [/system package get system version]

# --- Extract version parts (major.minor.patch) for comparison ---
:set PkgVer1 [:tonum [:pick $PkgVer 0 1]]
:set PkgVer2 [:tonum [:pick $PkgVer 2 4]]
:set PkgVer3 [:tonum [:pick $PkgVer 5 6]]

# --- Get current RouterBOARD firmware version (if applicable) ---
:set RbVer [/system routerboard get current-firmware]
:set RbVer1 [:tonum [:pick $RbVer 0 1]]
:set RbVer2 [:tonum [:pick $RbVer 2 4]]
:set RbVer3 [:tonum [:pick $RbVer 5 6]]

# --- Extract version parts of the target version (major.minor.patch) ---
:set TargetVer1 [:tonum [:pick $TargetVer 0 1]]
:set TargetVer2 [:tonum [:pick $TargetVer 2 4]]
:set TargetVer3 [:tonum [:pick $TargetVer 5 6]]

# --- Construct the download URL for the correct architecture and version ---
:set PackageURL ("https://download.mikrotik.com/routeros/" . $TargetVer . "/routeros-" . $Architecture . "-" . $TargetVer . ".npk")
:set urlToFetch "$PackageURL"

# --- Log detected information ---
:log info ("Target version: " . $TargetVer)
:log info ("Detected architecture: " . $Architecture)
:log info ("Current package version: " . $PkgVer)
:log info ("Download URL: " . $urlToFetch)

# --- Main logic: compare current and target versions ---
:if ($PkgVer != $TargetVer) do={

    :log info ("Package version does not match target version. Downloading package from " . $urlToFetch)
    /tool fetch url=$urlToFetch mode=https
    :delay 10s

    # --- Verify that the package file was downloaded successfully ---
    :if ([:len [/file find name=("routeros-" . $Architecture . "-" . $TargetVer . ".npk")]] = 0) do={
        :log error ("Package file routeros-" . $Architecture . "-" . $TargetVer . ".npk not found after download. Aborting.")
    } else={

        # --- Determine whether to downgrade or upgrade ---
        :if ($PkgVer1 > $TargetVer1 || $PkgVer2 > $TargetVer2 || $PkgVer3 > $TargetVer3) do={
            :log info ("Target version (" . $TargetVer . ") is older than current version (" . $PkgVer . "). Executing downgrade...")
            /system package downgrade
        } else={
            :log info ("Target version (" . $TargetVer . ") is newer than current version (" . $PkgVer . "). Rebooting to install...")
            :delay 3s
            /system reboot
        }
    }

} else={

    # --- If package version matches, check firmware version ---
    :if ($RbVer != $TargetVer) do={
        :log info ("RouterBOARD firmware (" . $RbVer . ") does not match target version (" . $TargetVer . "). Upgrading firmware...")
        /system routerboard upgrade
        :delay 3s
        /system reboot
    } else={
        :log info ("Software and firmware already match target version (" . $TargetVer . "). No action required.")
    }

}
Tommy.c
Posts: 77
Joined: Fri Jan 31, 2025 6:52 pm

Sun Oct 26, 2025 8:54 pm

Reviewing your code, I am not certain you are generating your URL correctly.

/tool fetch url=$urlToFetch mode=https
The Variable $urlToFetch is created by:
:set PackageURL ("https://download.mikrotik.com/routeros/" . $TargetVer . "/routeros-" . $Architecture . "-" . $TargetVer . ".npk")

When the packages I checked on MikroTik's website all look like this:
https://download.mikrotik.com/routeros/ ... -smips.npk

So I think you want it to look like this?
I would try:
:set PackageURL ("https://download.mikrotik.com/routeros/" . $TargetVer . "/routeros-" . $TargetVer . "-" . $Architecture . ".npk")

Or at least try logging what $PackageURL is being generated as...
dima1002
Posts: 5
Joined: Wed Aug 20, 2025 10:12 am

Mon Oct 27, 2025 11:56 am

So this is what the variables in Mikrotik look like after the script has been executed. If I use the link, I can download the file.
AP01.jpg
AP01.jpg (36.48 KiB) Viewed 7580 times
Tommy.c
Posts: 77
Joined: Fri Jan 31, 2025 6:52 pm

Mon Oct 27, 2025 2:43 pm

I am not certain where the problem is for you.

When I simplify your program down to this:

Code: Select all

:global PackageURL
:global urlToFetch
:set PackageURL "https://download.mikrotik.com/routeros/7.19.4/routeros-arm-7.19.4.npk"
:set urlToFetch "$PackageURL"
/tool fetch url=$urlToFetch mode=https
I get a download every time. Even when I point the fetch tool to $PackageURL vs $urlToFetch it works fine.

From the looks of it, when I run your script I get "no such item" errors. Might be from:

Code: Select all

:set PkgVer [/system package get system version]
I think you might want

Code: Select all

:set PkgVer [/system package get routeros version as-string]
When I update your script it downloads and does the update...

My suspicion is that RouterOS has had these commands changed between ROS 6 and 7. Possibly somewhere in ROS 7. I am currently running ROS 7.17 on my bench, so YMMV.
sc907
Posts: 1
Joined: Wed Dec 17, 2025 3:11 am

Wed Dec 17, 2025 7:47 am

I've written a method to specify the exact version of RouterOS to upgrade/downgrade via the the cli. The version is passed via a CLI argument and will also stage enabled packages: def-rosDownload.

Here are steps to 'rosDownload' via the CLI. You can also run these commands in bulk via the 'mass config push'. Once 'rosDownload' is installed and made persistent, you can run it as any normal command, see step #6, via a 'mass config push' if you don't want to move your equipment to the latest version.

1. Fetch def-rosDownload.rsc from GitHub

Code: Select all

/tool fetch url="https://raw.githubusercontent.com/seancrites/def-rosDownload/refs/heads/master/def-rosDownload.rsc" mode=https dst-path="def-rosDownload.rsc" output=file
2. Load the file as a script

Code: Select all

/system script add name=def-rosDownload source=[:file get def-rosDownload.rsc contents]
3. Run script to load global function

Code: Select all

/system script run def-rosDownload
4. Verify function registration

Code: Select all

/system script environment print where name=rosDownload
5. Make persistent on boot:

Code: Select all

/system scheduler add name="load-rosDownload" interval=0 on-event="/system script run def-rosDownload" start-time=startup
6. Use:

- Upgrade:

Code: Select all

$rosDownload 7.20.1
- Downgrade:

Code: Select all

$rosDownload 7.18.1
- LTS downgrade:

Code: Select all

$rosDownload 6.49.10
You will only be able to downgrade as low as your factory version. I've done some minimal testing and things should work, if not let me know.
Post Reply