Page 1 of 1

CRS - VLAN - Add untagged interfaces via script

Posted: Mon Jun 28, 2021 4:55 pm
by Zaesch
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.

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Thu Jul 01, 2021 4:25 pm
by rextended
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

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Thu Jul 01, 2021 4:33 pm
by rextended
... 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

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jul 02, 2021 9:52 am
by Zaesch
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 :-/

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jul 02, 2021 11:16 am
by rextended
In this case work because the command "set" accept array as results of nested commands

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jul 02, 2021 2:26 pm
by tdw
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

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jul 02, 2021 6:21 pm
by MangleRule
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 );
    };
}

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jul 02, 2021 6:57 pm
by rextended
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...

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Tue Jul 06, 2021 2:37 pm
by sirbryan
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 ]
}

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Tue Jul 06, 2021 3:43 pm
by rextended
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)*"

Re: CRS - VLAN - Add untagged interfaces via script

Posted: Fri Jan 26, 2024 6:33 pm
by joshhboss
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..