🧐 example of automating VLAN creation/removal/inspecting using $mkvlan & friends...

Adding a couple more function here — $mktrunk and $rmtrunk — that will “tag” bridge port with a provided . I actually skipped these in the initial posting since it’s actually not so straightforward to do this**…

Usage is simple:

$mktrunk - which will add the as tagged= on some /interface/bridge/vlan - it will either reuse an existing “static” one, or create/“add” a new /interface/bridge/vlan if the provide was not already there. The “add” assumes you have only one vlan-filtering=yes bridge, since it need to know which bridge to use.

To remove, it same syntax: $rmtrunk , which will remove the from any /interface/bridge/vlan that has as a member of vlan-ids= array. It has additional logic (marked, “# optional”) that will remove the /interface/bridge/vlan if it contains no tagged= or untagged= members at all (since $mktrunk might have created)

Feel free to adapt/improve, again just providing examples of a technique to do this. These don’t have a lot of bells-and-whistles to check things - since “clean” example is already complex… While I think they should be safe, so don’t try on a production router or anything like that without some testing.


:global mktrunk do={
:local bvid [/interface/bridge/vlan find dynamic=no vlan-ids=[:if ([:len [:find $“vlan-ids” $1]]) do={:return $“vlan-ids”}]]
:if ([:len $bvid]=0) do={
:set bvid [/interface/bridge/vlan add vlan-ids=$1 comment=“added by $0” bridge=[/interface/bridge/find vlan-filtering=yes disabled=no]]
}
/interface/bridge/vlan set $bvid tagged=([get $bvid tagged],$2)
}

:global rmtrunk do={
:local bvid [/interface/bridge/vlan find dynamic=no vlan-ids=[:if ([:len [:find $“vlan-ids” $1]]) do={:return $“vlan-ids”}]]
:local orig [/interface/bridge/vlan get $bvid tagged]
:local final [:toarray “”]
:foreach i in=$orig do={ :if ($i != “$2”) do={:set final ($final, $i)} }
/interface/bridge/vlan set $bvid tagged=$final
# optional, if there are no more tagged or untagged ports, remove bridge vlan itself
:if (([:len [/interface/bridge/vlan get $bvid tagged]]=0) and ([:len [/interface/bridge/vlan get $bvid untagged]]=0)) do={
/interface/bridge/vlan remove $bvid
}
# while mktrunk could take an array of interface, rmtrunk must be a single interface in $2
}

create

$mktrunk 123 ether4

update

$mktrunk 123 ether5

remove

$rmtrunk 123 ether5

note: array is only support on mktrunk, since it was automatic

$mktrunk 123 ("ether5","ether6")

but rmtrunk does NOT accept an array and will not find/remove anything

Couple notes:

  • again these assume 7.16+ (although should work in any version) — it does NOT add tagged=bridge when creating the interface. In 7.16 that will happen automatically, so theory is best to let it do that post-7.16.
  • the $mkvlan already has example of how to make an “access port” by setting PVID on /interface/bridge/port, using the “real” command seems appropriate and actually shown there

EX: So to make ‘ether3’ an access point, only the following additional command is:
/interface/bridge/port set [find interface=ether3] pvid=123 frame-types=allow-only-untagged

** @optio contributed the following exotic part to locate the .id of a /interface/bridge/vlan containing a VLAN ID – since [find vlan-ids=4] will not work, as vlan-ids is an array type, so 4 != (4) or 4 !=(1,2,3,4) either… See @pcunite’s 3 years-old prophetical Append Bridge vlan values post which has the discussion about script code above:

Long story short… how this works… while the find is running, essentially, a function is run on each item, and if the inner :find in index matches, the otter [find …] returns upon finding a matching <VLAN_ID>. And, note the colon : in “:find”, which finds the index in an array, which is not same as the plain “find”… And the careful $“dashed-attrbutes-use-quotes” & 0 == false, etc, etc… But it quite the dense scripting example for sure…