Mikrotik Scripting

At work we use several dozen Mikrotik routers. I am not a Windows user. I find Winbox on WINE painfully slow to use. Winbox is a crude and clunky tool when there are dozens of devices to support. We needed a way to automate common tasks.

I wanted a shell script that would interact with the devices and automate access. The script would not target complex commands or tasks. After several days I had a decent script that allowed me to run commands and simulate backups of multiple systems.

I am aware of tools that are designed to automate tasks across multiple systems, such as Ansible. While such tools might have future role at work, the shell script exercise provided me valuable insight on how to manage multiple systems. I think that is a good step long before trying automation tools.

The next step was writing a RouterOS script to perform backups. RouterOS scripting is not for the faint-hearted. After gathering notes and snippets from around the web I cobbled together something I could test.

I used my new shell script to upload the script to a test device. No errors. I used SSH to log into the device and ran the script.

/system script run backup.rsc


I presumed some kind of syntax error. I created a simple three line script. Same result. Nothing.

Scrounging around the web revealed how to list the scripts.

/system script print

The script was tagged invalid.

After more web searching I learned to edit the script directly in the device.

/system script edit test.rsc source

The crude Mikrotik text editor opened. The text editor is about as featureless as the infamous Windows notepad.

There was no content. Why was there no content? More head scratching.

I copied my three line script and pasted into the crude text editor. I saved the file. The /system script print command showed the script as valid.

I successfully ran the script.

Why was the same exact code not uploading?

I spent hours trying to resolve the question. Finally, ever so slowly, the proverbial light bulb above my head began glowing brighter. I realized the original design intent was scripts must be created on the device.

A couple more hours browsing the Mikrotik forum and shared scripts on the web confirmed my hunch. The crude featureless text editor supports copy and paste but such an exercise is inefficient when multiplied by many devices.

Such a design might make sense from the perspective of a single device, but not when needing to maintain dozens or hundreds of devices. A local central repository is needed for that, which means being able to upload scripts as they are are updated and tweaked.

My new shell script supports sending commands. I tinkered a while to get the shell script to upload the RouterOS script to the test device in a format that did not trigger the invalid tag. I needed help from others on the forums.

/system script add name=$UPLOAD_FILE policy=read,write source=[/file get $UPLOAD_FILE contents]

Nowhere on the Mikrotik wiki is any of this explained.

Scripts can be created locally on a device but cannot be downloaded to a central repository. Instead the script must be “exported.”

/system script export name=script.rsc

The command places the file in the device /file storage.

Then use scp or ftp to copy the file to a local off-device repository. The kicker with this export method is the file contents will contain \n and \r rather than true newlines or carriage returns. This makes the file difficult to edit on a personal computer.

Here is a simple script as the content appears using the crude Mikrotik text editor:

    :local myVar "test.rsc"
    :put "Testing script $myVar."
    :log info "Testing script $myVar."

Here is how the script looks after exporting with /system script export file=test.rsc:

    # aug/21/2019 13:42:48 by RouterOS 6.44.5
    # software id = 
    /system script
    add dont-require-permissions=no name=test.rsc owner=xxxxx policy=\
        read,write source=”:local myVar \"test.rsc\"\
        \n:put \"Testing script \$myVar.\"\
        \n:log info \"Testing script \$myVar.\"\

Some sed magic will clean the file.

There is a 4096 byte limitation when pushing scripts to a device. The file may be pushed to the/file storage repository but the RouterOS will not convert the file into a script. The contents must be manually copied and pasted into the crude text editor. No, really.

The RouterOS system is based on a Linux kernel, yet the date functions of the operating system do not use and do not recognize epoch dates. Calculating date related events is painful.

Face palm? Maybe. I don’t know. Certainly poorly designed software that is stuck in the 1990s. There really isn’t anything nice to say about such convoluted software.

Posted: Category: Usability Tagged: General

Next: Xfce Paper Cuts

Previous: The Linux Desktop