Community discussions

MikroTik App
 
S8T8
Member Candidate
Member Candidate
Topic Author
Posts: 144
Joined: Thu Sep 15, 2022 7:15 pm

:range command

Sat Mar 22, 2025 4:54 pm

Dear experts,
in the recent version v7.17, MikroTik introduced the :range function, which combined with the ability to use interface list, can improve the VLAN creation process.
I'm currently trying to add multiple interfaces as list members (/interface list member add interface=ether1,ether2... list=VLAN2),
the best I can think of is something like [:put "ether$[:range from=1 to=[:len [/interface ethernet find name~"ether"]]]"] but it's not enough to achieve the desired result.

Does anyone have a clever solution or can point me in the right direction?

TIA!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4732
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: :range command

Sun Mar 23, 2025 12:57 am


in the recent version v7.17, MikroTik introduced the :range function, which combined with the ability to use interface list, can improve the VLAN creation process.
I'm currently trying to add multiple interfaces as list members (/interface list member add interface=ether1,ether2... list=VLAN2),

Does anyone have a clever solution or can point me in the right direction?
I'm not sure [:range] is actually too useful here, since you still need some kind of loop to use it for VLAN config. For example,
:for i from=1 to=10 do={ /interface/list/member/add interface="ether$i" list=VLAN }
is same as
:foreach i in=[:range 1 10] do={ /interface/list/member/add interface="ether$i" list=VLAN }


I wrote a longer post about VLAN scripting:
🧐 example of automating VLAN creation/removal/inspecting using $mkvlan & friends...
viewtopic.php?t=214189
 
S8T8
Member Candidate
Member Candidate
Topic Author
Posts: 144
Joined: Thu Sep 15, 2022 7:15 pm

Re: :range command

Sun Mar 23, 2025 2:09 am

Thanks @Amm0!
I've already bookmarked your post but I was not able to understand/adopt to my situation where interfaces and VLANs are already set up.

One other thing I wasn't able to achieve (but is OT) is adding interface list as /interface bridge vlan tagged and removing ethernet interface already configured. Something like this:
(sorry I'm not expert enough to provide a working example)
:foreach VLAN2 in=[/interface bridge port get [find interface~"sfp|ether" and pvid=2] interface] do={
:local 2 [/interface bridge vlan get [find vlan-ids=2] untagged]
/interface bridge vlan set [find vlan-ids=2] untagged=($2 - $VLAN2)}

Is this included in your script?

By the way thanks again for your very informative posts and "diff RouterOS Commands" to comparing ROS changes, this is something MikroTik should provide by default.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4732
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: :range command

Sun Mar 23, 2025 4:34 pm

What specifically are you trying to update with scripting? VLAN bridge ports, bridge vlans, interface-list, etc & what settings etc?

Updating ("set") is more tricky than adding. Mainly because you need may need to add OR set, depending on what you mean by "update a VLAN".

More generally, learning how "find" works is probably the most important trick here. While :range can generate a list of numbers, and those could be VLAN IDs or the part of ether0X names etc., it's not really useful for "set". Since I mentioned that "set" only works on existing entires, if the "update" you were looking for is either adding a VLAN or setting something on existing. And "updating" things is where you need typically need some "find". The reason is "find" returns a list of .id's... and it's the "id" is what you need to use in any CLI "set" operation.

The /interface/bridge/vlan settings are particular hard in scripting, which is why all the code in the other thread appears more complex than needed... So dealing with trunk ports gets tricky. See Append Bridge vlan values(viewtopic.php?t=172732) which has more examples and context.
 
optio
Forum Guru
Forum Guru
Posts: 1079
Joined: Mon Dec 26, 2022 2:57 pm

Re: :range command

Sun Mar 23, 2025 5:44 pm

@S8T8 for adding members to interface list :range is not useful as @Amm0 mentioned because interface param for adding list interface member doesn't accepts array (and IMO it will be wrong usage to build interface names list with :range even if is accepted), just find interfaces with proper condition for interface name (can be some complex regex also) and iterate to add member...
For example in OP:
:foreach i in=[/interface/find name~"ether"] do={/interface/list/member/add interface=[/interface/get $i name] list=VLAN2}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 13022
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :range command

Mon Mar 24, 2025 3:22 pm

/interface ethernet find [:if ($name~"ether") do={/interface list member add interface=$name list="VLAN2"}]
(list must already exist)