Using scripting to overcome the inability to specify number ranges on the command line

From MikroTik Wiki
Revision as of 12:57, 8 March 2011 by Mattx86 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Admittedly, I (mattx86) have not really bothered to request that number ranges be allowed on the command line for operations such as disable, remove, etc., and as we don't yet have it available as of RouterOS version 5.0rc11, I have devised a method in order to do so.

What am I talking about?

Say we have the following list (the extra IP addresses were created for demonstration purposes):

[admin@MikroTik] > /ip address print
Flags: X - disabled, I - invalid, D - dynamic
 #   ADDRESS            NETWORK         INTERFACE
 0     lan-bridge
 1     fast-ipip
 2 D    dsl-pppoe
 3    lan-bridge
 4    lan-bridge
 5    lan-bridge
 6    lan-bridge

Now let's say that you want to disable numbers 3-6.

Rather than doing:

/ip address disable 3,4,5,6

Suppose you wanted to specify a range, such as:

/ip address disable 3-6

Guess what? It won't work (at least at the time of this writing). Yes, I realize it's not a big deal to specify "3,4,5,6" in this case, but what if you have a bigger list?

Well, thankfully(?), I've devised a method that works:

Compact Version:
(for direct use on the command line)

:local from 3;:local to 6;:local i 0;:foreach id in=[/ip address find] do={:if ($i >= $from && $i <= $to) do={/ip address disable $id}; :set i ($i+1)}

Expanded Version:
(for use in scripts, and to explain it better)

:local from 3  #update this
:local to 6      #update this
:local i 0         #set to zero because my list starts at zero
:foreach id in=[/ip address find] do={
	:if ($i >= $from && $i <= $to) do={
		/ip address disable $id
	:set i ($i+1)

Just change the from and to variables, replace "/ip address" in the foreach line to whatever list it is you're dealing with and change the command in the if block to whatever command(s) you need to do. (You can even use the set operation, e.g.: /ip address set $id disabled=yes)