Scriptlet: Find Default Route Interface Names (and a free licence!)

Today I’m giving you the task of reviewing and improving a small script I’ve written, and one of you will win a free Level 4 RouterOS licence. 

Background: I had need of a script to find the interfaces associated with any default routes in order to create matching firewall entries, and it had to work with RouterOS v6.

This script searches through any default route (dst-address= and adds it to an array so long as the interface can’t already be found in the array. I don’t often use arrays in MikroTik so the first version has a search function that doesn’t loop through the array, but instead just converts it to a string again to run the find command (Line 8)

Here’s the script:

:local gwintarray "";
:local counter "";
:local counter2 "";
:local intfinder "";
:foreach counter in=[/ip route find where dst-address=""] do={
 :set $intfinder [:tostr [/ip route get $counter gateway-status]]
 :set $intfinder [:pick $intfinder ([:find $intfinder "via"]+5) [:len $intfinder]]
 :if ([:find [:tostr $gwintarray] $intfinder] > 0) do={
 :put "Found existing int";
 } else {
 :set $gwintarray ($gwintarray , "$intfinder");
 :put $intfinder;

Now here’s the challenge.

Anyone who can submit a working version of this script where line 8 is replaced with an array-based search will go into the draw to win a free Level 4 RouterOS licence.

This competition will close on Friday  24th October at 12 midday Australian Eastern Standard Time.

Anyone who has been on the BrothersWISP podcast is unable to compete sorry guys 🙂

2 thoughts on “Scriptlet: Find Default Route Interface Names (and a free licence!)

  1. Better late than never, at least just for the exercise. Tried on 6.24. Will break if multiple gateways are assigned to a single route.

    /ip route {
        :local gwintarray;
        :local counter;
        :local intfinder;
        :foreach counter in=[find dst-address=] do={
            :set $intfinder [:tostr [get $counter gateway-status]]
            :local posVia [:find $intfinder " reachable via "];
            :if ($posVia > 1) do={
                :set $intfinder [:pick $intfinder ($posVia + 16) [:len $intfinder]]
                :if ([:find $gwintarray $intfinder] > 0) do={
    #                :put "Found existing int";
                } else {
                    :set $gwintarray ($gwintarray, $intfinder);
    #                :put $intfinder;
        :put $gwintarray;

Leave a Reply