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=0.0.0.0/0) 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="0.0.0.0/0"] 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 🙂

Share this: Facebook

Reddit

Twitter

Email

Print

