MikroTik Scripting Basics – Automate Your Router Tasks Print

  • Mikrotik
  • 27

Hello there, it’s Corels from Emmanuel Corels Creatives! Today, we’re going to step away from GUI-based configurations and dive into the powerful world of MikroTik scripting. Scripting in RouterOS lets you automate tasks, streamline troubleshooting, and even perform routine maintenance without manual intervention every time. In this guide, I’ll explain what scripting is, break down common script commands, and share real-world examples to show you how to use them effectively.


Why Learn MikroTik Scripting?

Imagine if your router could:

  • Automatically back up its configuration every night.
  • Check if an interface goes down and send you an alert.
  • Loop through all interfaces to report their status, so you always know if something isn’t running as expected.

Scripting turns these ideas into reality. Not only does it save you time, but it also makes your network management more reliable and consistent. Once you get the hang of it, you’ll wonder how you ever managed without it!


The Basics of MikroTik Scripting

MikroTik scripts are essentially sequences of RouterOS commands that the system executes sequentially. They can include loops, conditionals, and variables—much like traditional programming languages.

Let’s explore some fundamental concepts:

1. Variables

Variables let you store and reuse values. For example:

:local myInterface "ether1"

This command creates a local variable called myInterface with the value "ether1". You can use this variable later in your script to refer to that interface.

Real-World Example:
If you need to run checks on a particular interface multiple times in your script, storing its name in a variable makes your code cleaner and easier to update.

2. Loops

Loops let you perform repetitive tasks. The :foreach loop is commonly used in MikroTik scripts.

:foreach i in=[/interface find] do={
    :put ([/interface get $i name] . " status: " . [/interface get $i running])
}

Explanation:

  • This script loops through every interface on your router.
  • For each interface, it prints the interface’s name and whether it’s running.

Real-World Example:
You might use a loop to generate a report of all interfaces and log it daily, ensuring you quickly spot if an interface goes down.

3. Conditionals

Conditional statements let your script make decisions. For instance:

:if ( [/interface get [find default-name=ether1] running] = false ) do={
    :log warning "Ether1 is down!"
} else={
    :log info "Ether1 is up and running."
}

Explanation:

  • This checks if ether1 is running.
  • If it’s not, the script logs a warning; otherwise, it logs that everything is fine.

Real-World Example:
Use conditionals in your health-check scripts to automatically alert you via log messages or even trigger an email if an interface goes down.

4. Running Commands

Simply including a command in a script executes it. For example:

/system backup save name="daily-backup"

Real-World Example:
Schedule this command in a script to automatically back up your configuration every day at a specific time.


Putting It All Together: A Practical Script

Let’s create a simple script that checks the status of all interfaces and logs a message if any interface is down. Save this as a script called “CheckInterfaces.”

:foreach iface in=[/interface find] do={
    :local name [/interface get $iface name];
    :local status [/interface get $iface running];
    :if ($status = false) do={
        :log warning ("Interface " . $name . " is down!");
    } else={
        :log info ("Interface " . $name . " is running.");
    }
}

Step-by-Step Explanation:

  • :foreach iface in=[/interface find] do={ ... }
    This loops through every interface on your router.
  • :local name [/interface get $iface name]
    Retrieves the name of the current interface and stores it in the variable name.
  • :local status [/interface get $iface running]
    Gets the running status (true/false) of the interface.
  • :if ($status = false) do={ ... } else={ ... }
    Checks if the interface is down; if so, logs a warning; otherwise, logs that it’s running.

Real-World Use:
Run this script daily (using the Scheduler) so you get immediate feedback on your router’s health. If any interface goes down unexpectedly, you’ll see a clear warning in the log.


Scheduling Your Script

To automate this health-check, you can schedule it to run at regular intervals:

  1. In WinBox, go to System → Scheduler.
  2. Click “+” to add a new scheduled task.
  3. Name: Interface_Check
  4. Start Time: Set a time (e.g., 00:00:00 for midnight).
  5. Interval: 1d (daily) or shorter if needed.
  6. On Event:
    /system script run CheckInterfaces
    
  7. Click OK.

This ensures that your router automatically checks all interfaces every day.


Final Thoughts

MikroTik scripting might seem daunting at first, but with a few fundamental concepts—variables, loops, conditionals—you can automate many routine tasks and make your network management much more efficient. Whether it’s checking interface statuses, automating backups, or generating reports, scripting is your gateway to a more proactive and hands-off approach to router administration.

Practice these commands in a lab environment, experiment with your own scripts, and soon you’ll be comfortable enough to create more complex automation routines tailored to your needs. If you have any questions or need further guidance, feel free to reach out. Happy scripting, and here’s to making your MikroTik router work smarter for you!


Explained with practical clarity by
Corels – Admin, Emmanuel Corels Creatives


Does this help?

« Back