Community discussions

MikroTik App
 
MakroTok
newbie
Topic Author
Posts: 28
Joined: Wed Dec 04, 2019 4:36 pm

"Dynamic" repetitive configuration of interfaces

Sun Feb 12, 2023 1:54 am

Some long time ago I have written a configuration (with a lot of help from the forum) which was initially targeting the "hEX RB750GR3".
Now people started using it also with "HAP ac lite". The main difference is that the former has 5 ports and the later only 4.

As the people using the configuration are only "consuming" it I was wondering if the difference could be somehow automated away so that they don't have to care

The original code in question is:
/interface vlan
add interface=ether2 name=vlan7_ether2 vlan-id=7
add interface=ether3 name=vlan7_ether3 vlan-id=7
add interface=ether4 name=vlan7_ether4 vlan-id=7
add interface=ether5 name=vlan7_ether5 vlan-id=7

/interface bridge
add name=bridge_pppoe arp=proxy-arp
port add bridge=bridge_pppoe interface=vlan7_ether2
port add bridge=bridge_pppoe interface=vlan7_ether3
port add bridge=bridge_pppoe interface=vlan7_ether4
port add bridge=bridge_pppoe interface=vlan7_ether5
So all ether* interfaces expect ether1 (because it acts as WAN) are to be considered.

I've seen some usages of :foreach loops but am really just starting. After some fiddling I came up with:
/interface bridge
add name=bridge_pppoe arp=proxy-arp

{
    :local interfaces [/interface print as-value where name~"ether" and name!="ether1"];
    :foreach interface in=$interfaces do={
        :local ifName   ($interface->"name");
        :local vlanName ("vlan7_" . $ifName);
        /interface vlan add interface=$ifName name=$vlanName vlan-id=7
	/interface bridge port add bridge=bridge_pppoe interface=$vlanName
    }
}
It seems to do what I'd expect it to do but as it's my first script I'm a bit unsure if it's the right way to do it. Any suggestions?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: "Dynamic" repetitive configuration of interfaces  [SOLVED]

Sun Feb 12, 2023 4:58 am

Do not use print on script that not requre print something on screen.
Do not use word that also represent some item with same name.
/interface bridge
add name="bridge_pppoe" arp=proxy-arp

/interface
:foreach item in=[find where type="ether" and default-name!="ether1"] do={
    :local ifName   [get $item name]
    :local vlanName "vlan7_$ifName"
    vlan add name=$vlanName vlan-id=7 interface=$ifName
    bridge port add bridge="bridge_pppoe" interface=$vlanName
}
 
MakroTok
newbie
Topic Author
Posts: 28
Joined: Wed Dec 04, 2019 4:36 pm

Re: "Dynamic" repetitive configuration of interfaces

Mon Feb 13, 2023 1:59 pm

Many thanks for the feedback and the useful tipps. I directly adopted your version :D

Who is online

Users browsing this forum: marcelofares and 25 guests