Community discussions

MikroTik App
 
Zaesch
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Thu Apr 16, 2009 12:43 pm

CRS - VLAN - Add untagged interfaces via script

Mon Jun 28, 2021 4:55 pm

Hi,

I want to write a default configuration script to configure all SFP+ interfaces of a CRS326-24S+-2Q+RM with an access vlan. The plan:
  • set pvid=100 for all SFP+ interfaces (check - works)
  • add all interfaces with pvid=100 to the bridge vlan interface as untagged (fails :-( )

The following command gets the interface IDs for all interfaces with pvid 100:
:put [/interface bridge port find where pvid=100]

Another command gets the name of the interface (which is need to be set in the /interface bridge vlan ... untagged=INTERFACE-NAME command):
:put [/interface bridge port get 1 vlaue-name=interface]

Each command works fine, but combined they fail with "invalid internal item number". The goal was to use the list from this command as input for teh /interface bridge vlan ... untagged=... command:
:put [/interface bridge port get [/interface bridge port find where pvid=100] vlaue-name=interface]

What do I miss?

:put is added to see the results in the terminal.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Thu Jul 01, 2021 4:25 pm

the get 1 vlaue-name is valid only on terminal print,
is not the interfae ID or what'ever other, just for print order,
you must use the .id as appear on :put like *1

you can not mix request:
the [/interface bridge port find where pvid=100] return one array, empty, with one or with more than one elements
do not reply with .id usable for successive request
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Thu Jul 01, 2021 4:33 pm

... I want to write a default configuration script to configure all SFP+ interfaces of a CRS326-24S+-2Q+RM with an access vlan ...
How do it:

reset the switch to default without "default config",

configure the switch as wanted with winbox,

when done create an export with /export

now all instructions to be placed on script for reconfig similar device is ready to be copied&pasted
 
Zaesch
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Thu Apr 16, 2009 12:43 pm

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jul 02, 2021 9:52 am

Thanks for the replys rextended.
The click and point orgy in Winbox is the reason to write such a script. I don't want to do it even one time ;-)

I thought it would work with nested commands, because in other cases it does. Like
/interface bridge port set pvid=111 [/interface bridge port find where pvid=100]
But the get command works diffrent - obviously :-/
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jul 02, 2021 11:16 am

In this case work because the command "set" accept array as results of nested commands
 
tdw
Forum Guru
Forum Guru
Posts: 1843
Joined: Sat May 05, 2018 11:55 am

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jul 02, 2021 2:26 pm

There isn't actually any need to set untagged= membership under /interface bridge vlan, they will be added dynamically from the pvid= settings under /interface bridge port
 
User avatar
MangleRule
Frequent Visitor
Frequent Visitor
Posts: 61
Joined: Mon May 07, 2018 1:05 am

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jul 02, 2021 6:21 pm

The VLAN tagged and untagged fields are comma separated values. So if you want to build up a comma separated list you can do something like this.
{
    /interface ethernet {
        :local SFPInterfaces [ find default-name~"sfp*" ];
        :local InterfaceList;

        :foreach i,Interface in=$SFPInterfaces do={
            :local InterfaceName [ get $Interface name ];

            :if ( i = 0 ) do={
                :set $InterfaceList ($InterfaceList . $InterfaceName );
            } else={
                :set $InterfaceList ( $InterfaceList . "," . $InterfaceName );
            };
        };

        :log info ( $InterfaceList );
    };
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jul 02, 2021 6:57 pm

search tag # rextended comma separated ethernet list

Shorter and clearer:
{
    /interface ethernet
    :local InterfaceList ""
    :local separator ""
    :foreach i,Interface in=[ find where default-name~"(combo|ether|sfp)*" ] do={
        :set InterfaceList "$InterfaceList$separator$[ get $Interface name ]"
        :if ($i = 0) do={ :set separator "," }
    }

    :log info $InterfaceList
}

And thanks for the hint to use "i", I never stop discovery new things, thanks to all...
Last edited by rextended on Wed Mar 15, 2023 1:03 pm, edited 2 times in total.
 
User avatar
sirbryan
Member
Member
Posts: 303
Joined: Fri May 29, 2020 6:40 pm
Location: Utah
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Tue Jul 06, 2021 2:37 pm

Shorter and clearer:
{
    /interface ethernet
    :local InterfaceList ""
    :local separator ""
    :foreach i,Interface in=[ find where default-name~"sfp*" ] do={
        :set $InterfaceList "$InterfaceList$separator$[ get $Interface name ]"
        :if ($i = 0) do={ :set separator "," }
    }

    :log info $InterfaceList
}

And thanks for the hint to use "i", I never stop discovery new things, thanks to all...
You're trying to set separator out of scope:
{...         :set $InterfaceList "$InterfaceList$separator$[ get $Interface name ]"                        
{...         :if ($i = 0) do={ :set separator "," }                                
syntax error (line 3 column 32)
Quick and dirty, this worked for me and allowed me to tag dozens of ports into dozens of VLANs by repeating variations of the last bridge vlan command from the CLI for the other VLANs without having to rerun the script. In a "production" setup, you probably wouldn't want to keep the variables global.

Don't forget to add the bridge to InterfaceList if you want the bridge to be tagged on those VLANs as well.
{
    /interface ethernet
    :global InterfaceList ""
    :global separator ""
    :foreach i,Interface in=[ find where default-name~"ether*|sfp*" ] do={
        :set $InterfaceList "$InterfaceList$separator$[ get $Interface name ]"
        :if ($i = 0) do={ :set separator "," }
    }

    :put $InterfaceList
    /interface bridge vlan set tagged=$InterfaceList [ find where vlan-ids=140 ]
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: CRS - VLAN - Add untagged interfaces via script

Tue Jul 06, 2021 3:43 pm

I do not set the separator out of scope,
it is you who did not copy the { }, if there are, there must be a reason.
It is logical that it gives you error if you do not copy exactly.

{...         :if ($i = 0) do={ :set separator "," }
^--- this is a clue you did not put { at the beginning

for add also all type of physical ethernet is better this regular experssion than only "sfp*" :
default-name~"(combo|ether|sfp)*"
 
joshhboss
Member Candidate
Member Candidate
Posts: 273
Joined: Thu Aug 01, 2019 2:13 pm

Re: CRS - VLAN - Add untagged interfaces via script

Fri Jan 26, 2024 6:33 pm

I do not set the separator out of scope,
it is you who did not copy the { }, if there are, there must be a reason.
It is logical that it gives you error if you do not copy exactly.

{...         :if ($i = 0) do={ :set separator "," }
^--- this is a clue you did not put { at the beginning

for add also all type of physical ethernet is better this regular experssion than only "sfp*" :
default-name~"(combo|ether|sfp)*"

Scripts here are much cleaner then either of what I do.. what I do does save time but still not as clean as those..

one I use all the time to REMOVE ALL TAGS!
/interface bridge vlan 
:local iface "sfp-sfpplus4"; # put here the interface you want to remove
# on next row, set your conditions
:foreach i in=[find where ((vlan-ids>=10 and vlan-ids<=199) || (vlan-ids>=200 and vlan-ids<=1703))] do={
    # here goes magic
    set $i tagged=([:pick [get $i tagged] 0 [:find [get $i tagged] $iface +1]],[:pick [get $i tagged] ([:find [get $i tagged] $iface -1] + 1) [:len [get $i tagged]]])
}
These scripts are much cleaner for the interface.. for tagging.. (rex,sirbryan combo)
{
    /interface ethernet
    :global InterfaceList ""
    :global separator ""
    :foreach i,Interface in=[ find where default-name~"ether*|sfp*" ] do={
        :set $InterfaceList "$InterfaceList$separator$[ get $Interface name ]"
        :if ($i = 0) do={ :set separator "," }
    }

    :put $InterfaceList
    /interface bridge vlan set tagged=$InterfaceList [ find where vlan-ids=140 ]
}



Mine I have to mess with all the time.. but still better then messing with them one by one.. mine is below..
set [find where (vlan-ids>="201" and vlan-ids<="290")] tagged=sfp-sfpplus1,sfp-sfpplus4,sfp1,sfp2,sfp3,sfp4,sfp5,sfp6,sfp7,sfp8,sfp9,sfp10,sfp11,sfp12,sfp13,sfp14,sfp15,sfp16,sfp17,sfp18,sfp19,sfp20
Not really even a script.. just did what I wanted it too.. lol

Im going to mash these around and try and improve the ones im using..

Amazing what you guys put together..

Who is online

Users browsing this forum: No registered users and 27 guests