Scripting With Mikrotik
Scripting With Mikrotik
• Monitoring of router.
Basic Scripting Commands/Syntax
• delay • environment
• len • error
• typeof • parse
• time • set
Advanced Scripting : Examples
Example of a Loop :local smscount;
• Deleting all received :set smscount [:tool sms inbox print count-only;];
:local i;
messages from inbox.
:set i 1;
:tool sms inbox print append file="text log";
:for j from=$i to=$smscount step=1 do={:tool sms
inbox print; :tool sms inbox remove numbers=0;};
:set smscount [tool sms inbox print count-only];
Advanced Scripting Another Example
• Blocking websites using :local list
DNS address :local comment
• Creating the address list. :local newip
• Creating the Firewall Rule.. :local oldip
• Picking each entry in the
“host” list. # Loop through each entry in the address list.
• Resolving the DNS given in :foreach i in=[/ip firewall address-list find] do={
the comment :set list [:pick [/ip firewall address-list get \$i list] 0 5]
• Putting the address in the # If they're 'host_', then we've got a match - process it
address list. :if (\$list = \"host_\") do={
:set comment [/ip firewall address-list get \$i comment]
/ip firewall address-list add address=0.0.0.0
comment=www.blinknet.com :set oldip [/ip firewall address-list get \$i address]
list=host_blinknet # Resolve it and set the address list entry accordingly.
/ip firewall filter add chain=forward dst- : if (\$newip != \$oldip) do={:set newip [:resolve \$comment]
address-list=host_blinknet action=drop /ip firewall address-list set \$i address=\$newip}} }
Advanced Scripting : Examples
Example of Fetching System Information
:local cpuload;
• Setting :set cpuload [system resource get cpu-load];
:local uptime;
Parameters :set uptime [system resource get uptime ];
to variables. :local version;
:set version [system resource get version ];
• Displaying in :local freemem;
:set freemem [system resource get free-memory ];
console :local freq;
and/or Logs. :set freq [system resource get cpu-frequency ];
:local sysid;
• Sending the :set sysid [system identity get name];
fetched data :put "Router Name: $sysid | Up Time: $uptime | Cpu Load: $cpuload
% | Version: $version | Free Memory: $freemem | CPU Frequency:
via SMS to $freq ";
Admin :tool sms send port=Airtel channel=3 phone-number="9051634972"
message="Router Name: $sysid | Up Time: $uptime | Cpu Load:
$cpuload % | Version: $version | Free Memory: $freemem | CPU
Frequency: $freq ";
What Can Be Achieved
• Automated system monitoring.
• Extending Functionality.
Questions???