travelling was more time-consuming than expected but here we go, my configuration/script
I modified your code slightly:
- keeping space works well and readability is maintained
- some variables can be local scope but not all, whyever?
# name the device being configured
/system identity set name="AP+ES_GF_MT-hAP-ac3"
# create one bridge, with defaults only
/interface bridge
add name=bridge-VLANs
# add VLANs
/interface vlan
add interface=bridge-VLANs vlan-id=999 name=VLAN_MGMT comment="NETWORK DEVICES MANAGEMENT VLAN"
# VLAN interface for L3 Management Access
/ip address
add address=10.10.9.001/24 interface=VLAN_MGMT comment="NETWORK DEVICES MANAGEMENT VLAN" --> CR_2B_MT-CCR1009-8G-1S-1S+
add address=10.10.9.002/24 interface=VLAN_MGMT comment="NETWORK DEVICES MANAGEMENT VLAN" --> CS-PoE_2B_MT-CRS328-24P-4S+
# The route to the router to allow L3 communication.
/ip route
add gateway=10.10.9.1 distance=1
# DNS server, to allow ROS to resolve DNS queries from the CPUto fetch updates etc.
/ip dns
set allow-remote-requests=no servers="10.10.9.1"
#ES_GF-E_MT-hEX-S
/interface ethernet
set comment="TP, VLAN-TRUNK TO CORE-SWITCH-SFP" [find name=sfp1]
set comment="UP, don't touch (temporarily)" [find name=ether1]
set comment="UP, VLAN_USER1, OFFICE, UNMANAGED SWITCH" [find name=ether2]
set comment="UP, VLAN_USER2, KIDS" [find name=ether3]
set comment="UP, VLAN_USER2, LIVING ROOM" [find name=ether4]
set comment="UP, don't touch (temporarily)" [find name=ether5]
## START OF SCRIPT
#creating variable per VLAN and other variables
:global "VLAN_BKHOLE" { vlanid=001; name="VLAN_BKHOLE" ; comment="BLACKHOLE" }
:global "VLAN_SYS" { vlanid=008; name="VLAN_SYS" ; comment="SYS" }
:global "VLAN_GUEST" { vlanid=011; name="VLAN_GUEST" ; comment="GUEST" }
:global "VLAN_USER1" { vlanid=110; name="VLAN_USER1" ; comment="USER 1,all devices in one vlan" }
:global "VLAN_USER2" { vlanid=120; name="VLAN_USER2" ; comment="USER 2,all devices in one vlan" }
:global filterUP "^UP, VLAN_USER"
:global BridgeName "bridge-VLANs"
:global result
#show configuration before script execution
interface ethernet print where comment~"^UP."
interface bridge port print
#clear anything in /interface bridge port if there is an existing configuration
:put "CLEAR EXISTING BRIDGE PORT CONFIGURATION
:foreach Interface in=[ /interface bridge port find ] do={ /
/interface bridge port remove $Interface
}
#show that /interface bridge port is empty
/interface bridge port print
#add Bridge Ports according to the comment of the interfaces. PVID is set as well.
:put "STARTING SCRIPT-BASED BRIDGE PORT CREATION
:foreach Interface in=[ /interface ethernet find where comment~$filterUP ] do={ /
:local readcomment ( [ :toarray ([/interface ethernet get $Interface]->"comment" ) ]->1 )
:local readthisfield "vlanid"
[:parse ("global \"$readcomment\";:global result (\$\"$readcomment\"->\"$readthisfield\")")]
/interface bridge port add interface=$Interface bridge=$BridgeName pvid=$result
}
"show result
:put "SHOW RESULTS"
/interface ethernet print where comment~"^UP,"
/interface bridge port print
/interface bridge port print where pvid=110
/interface bridge port print where pvid=120
and the reduced code generated OUTPUT:
/interface ethernet print where comment~"^UP,"
Flags: R - RUNNING; S - SLAVE
Columns: NAME, MTU, MAC-ADDRESS, ARP, SWITCH
# NAME MTU MAC-ADDRESS ARP SWITCH
;;; UP, don't touch (temporarily)
0 R ether1 1500 08:55:31:0F:52:97 enabled switch1
;;; UP, VLAN_USER1, OFFICE, UNMANAGED SWITCH
1 S ether2 1500 08:55:31:0F:52:98 enabled switch1
;;; UP, VLAN_USER2, KIDS
2 S ether3 1500 08:55:31:0F:52:99 enabled switch1
;;; UP, VLAN_USER2, LIVING ROOM
3 S ether4 1500 08:55:31:0F:52:9A enabled switch1
;;; UP, don't touch (temporarily)
4 ether5 1500 08:55:31:0F:52:9B enabled switch1
CLEAR EXISTING BRIDGE PORT CONFIGURATION
STARTING SCRIPT-BASED BRIDGE PORT CREATION
SHOW RESULTS
/interface ethernet print where comment~"^UP,"
Flags: I - INACTIVE; H - HW-OFFLOAD
Columns: INTERFACE, BRIDGE, HW, PVID, PRIORITY, PATH-COST, INTERNAL-PATH-COST, HORIZON
# INTERFACE BRIDGE HW PVID PRIORITY PATH-COST INTERNAL-PATH-COST HORIZON
0 IH ether2 bridge-VLANs yes 110 0x80 10 10 none
1 IH ether3 bridge-VLANs yes 120 0x80 10 10 none
2 IH ether4 bridge-VLANs yes 120 0x80 10 10 none
[admin@MikroTik] /interface/mesh> /interface bridge port print where pvid=110
Flags: I - INACTIVE; H - HW-OFFLOAD
Columns: INTERFACE, BRIDGE, HW, PVID, PRIORITY, PATH-COST, INTERNAL-PATH-COST, HORIZON
# INTERFACE BRIDGE HW PVID PRIORITY PATH-COST INTERNAL-PATH-COST HORIZON
0 IH ether2 bridge-VLANs yes 110 0x80 10 10 none
[admin@MikroTik] /interface/mesh> /interface bridge port print where pvid=120
Flags: I - INACTIVE; H - HW-OFFLOAD
Columns: INTERFACE, BRIDGE, HW, PVID, PRIORITY, PATH-COST, INTERNAL-PATH-COST, HORIZON
# INTERFACE BRIDGE HW PVID PRIORITY PATH-COST INTERNAL-PATH-COST HORIZON
1 IH ether3 bridge-VLANs yes 120 0x80 10 10 none
2 IH ether4 bridge-VLANs yes 120 0x80 10 10 none