Conditional scripts

< All Topics

The Mikrotik scripting language permit to use conditional statements that can be used when you create your own configuration template.

Such conditional statement can include one or several rules among the following elements :

  • “If (true) do (instruction X) else (instruction Y)”
  • Loops : “if”, “for”, “foreach”
  • maths operators : use directly operators like “+” “-” “*” “/”, relational operators “<” “>” “=” or logical operators
  • and many other possibilities

Here is an example with a conditional step to restrict the configuration operation to a specific condition : before configuring the RADIUS server IP address, you can check first whether the current value is the same or not. Note that you need to define the variables of your configuration with a “local” (limited to a section of the configuration) or “global” ( valid for the whole configuration).

# Mikrotik Set RADIUS Client with my-AAA 1.2.3.4
:local RadiusIP 1.2.3.4;
:local Radiussec mysecret;
:local Radiustout 00:00:01;
:local radiusID [/radius find comment=”my-AAA”];
:local currentIP [/radius get $radiusID address];
#
Check if RADIUS client already configured
:if ($RadiusIP != $currentIP) do={
/radius set service=hotspot $radiusID address=$RadiusIP secret=$Radiussec timeout=$Radiustout;
/log info “radius my-AAA updated”;
} else {/log info “no update my-AAA RADIUS already set”;}

Conditional statements are particularly useful to avoid creating multiple entries related to the same configuration.

Table of Contents