mirror of
https://git.eworm.de/cgit/routeros-scripts
synced 2025-08-01 16:54:38 +02:00
add global-functions.d/bridge-port-vlan
This commit is contained in:
parent
a9f81c7a14
commit
b6215ba958
8 changed files with 159 additions and 3 deletions
|
@ -201,6 +201,11 @@ Available Scripts
|
||||||
[comment]: # (* learn-mac-based-vlan)
|
[comment]: # (* learn-mac-based-vlan)
|
||||||
[comment]: # (* manage-umts)
|
[comment]: # (* manage-umts)
|
||||||
|
|
||||||
|
Available modules
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
* [Manage VLANs on bridge ports](doc/global-functions.d/bridge-port-vlan.md)
|
||||||
|
|
||||||
Contact
|
Contact
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
@ -76,6 +76,11 @@ More configuration can be loaded by setting `BridgePortTo`:
|
||||||
* Interfaces `en1` and `en2` are unchanged.
|
* Interfaces `en1` and `en2` are unchanged.
|
||||||
* Interface `en3` is put in bridge `br-intern`.
|
* Interface `en3` is put in bridge `br-intern`.
|
||||||
|
|
||||||
|
See also
|
||||||
|
--------
|
||||||
|
|
||||||
|
* [Manage VLANs on bridge ports](global-functions.d/bridge-port-vlan.md)
|
||||||
|
|
||||||
---
|
---
|
||||||
[◀ Go back to main README](../README.md)
|
[◀ Go back to main README](../README.md)
|
||||||
[▲ Go back to top](#top)
|
[▲ Go back to top](#top)
|
||||||
|
|
83
doc/global-functions.d/bridge-port-vlan.md
Normal file
83
doc/global-functions.d/bridge-port-vlan.md
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
Manage VLANs on bridge ports
|
||||||
|
============================
|
||||||
|
|
||||||
|
[◀ Go back to main README](../../README.md)
|
||||||
|
|
||||||
|
🛈 This module can not be used on its own but requires the base installation.
|
||||||
|
See [main README](../../README.md) for details.
|
||||||
|
|
||||||
|
Description
|
||||||
|
-----------
|
||||||
|
|
||||||
|
This module and its function are supposed to handle VLANs on bridge ports.
|
||||||
|
|
||||||
|
Requirements and installation
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
Just install the module:
|
||||||
|
|
||||||
|
$ScriptInstallUpdate global-functions.d/bridge-port-vlan;
|
||||||
|
|
||||||
|
Configuration
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Using named VLANs you have to add comments in bridge vlan menu:
|
||||||
|
|
||||||
|
/ interface bridge vlan add bridge=bridge comment=intern tagged=br-local vlan-ids=10;
|
||||||
|
/ interface bridge vlan add bridge=bridge comment=geust tagged=br-local vlan-ids=20;
|
||||||
|
/ interface bridge vlan add bridge=bridge comment=extra tagged=br-local vlan-ids=30;
|
||||||
|
|
||||||
|
The configuration goes to ports' comments (`/ interface bridge port`).
|
||||||
|
|
||||||
|
/ interface bridge port add bridge=bridge comment="default=dhcp-client, alt=guest" disabled=yes interface=en1;
|
||||||
|
/ interface bridge port add bridge=bridge comment="default=intern, alt=guest, extra=30" interface=en2;
|
||||||
|
/ interface bridge port add bridge=bridge comment="default=guest, extra=extra" interface=en3;
|
||||||
|
|
||||||
|
Also dhcp client can be handled:
|
||||||
|
|
||||||
|
/ ip dhcp-client add comment="toggle with bridge port" disabled=no interface=en1;
|
||||||
|
|
||||||
|
Add a scheduler to start with default setup on system startup:
|
||||||
|
|
||||||
|
/ system scheduler add name=bridge-port-vlan on-event=":global GlobalFunctionsReady; :while (\$GlobalFunctionsReady != true) do={ :delay 500ms; }; :global BridgePortVlan; \$BridgePortVlan default;" start-time=startup;
|
||||||
|
|
||||||
|
Usage and invocation
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
The usage examples show what happens with the configuration from above.
|
||||||
|
|
||||||
|
Running the function `$BridgePortVlan` with parameter `default` applies all
|
||||||
|
configuration given with `default=`:
|
||||||
|
|
||||||
|
$BridgePortVlan default;
|
||||||
|
|
||||||
|
For the three interfaces we get this configuration:
|
||||||
|
|
||||||
|
* The special value `dhcp-client` enables the dhcp client for interface `en1`. The bridge port entry is disabled.
|
||||||
|
* Primary VLAN `intern` (ID `10`) is configured on `en2`.
|
||||||
|
* Primary VLAN `guest` (ID `20`) is configured on `en3`.
|
||||||
|
|
||||||
|
Running the function `$BridgePortVlan` with parameter `alt` applies all
|
||||||
|
configuration given with `alt=`:
|
||||||
|
|
||||||
|
$BridgePortVlan alt;
|
||||||
|
|
||||||
|
* Primary VLAN `guest` (ID `20`) is configured on `en1`, dhcp client for the interface is disabled.
|
||||||
|
* Primary VLAN `guest` (ID `20`) is configured on `en2`.
|
||||||
|
* Interface `en3` is unchanged, primary VLAN `guest` (ID `20`) is unchanged.
|
||||||
|
|
||||||
|
Running the function `$BridgePortVlan` with parameter `extra` applies another
|
||||||
|
configuration:
|
||||||
|
|
||||||
|
* Interface `en1` is unchanged.
|
||||||
|
* Primary VLAN `extra` (via its ID `30`) is configured on `en2`.
|
||||||
|
* Primary VLAN `extra` (ID `30`) is configured on `en3`.
|
||||||
|
|
||||||
|
See also
|
||||||
|
--------
|
||||||
|
|
||||||
|
* [Manage ports in bridge](../bridge-port.md)
|
||||||
|
|
||||||
|
---
|
||||||
|
[◀ Go back to main README](../../README.md)
|
||||||
|
[▲ Go back to top](#top)
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
# Make sure all configuration properties are up to date and this
|
# Make sure all configuration properties are up to date and this
|
||||||
# value is in sync with value in script 'global-functions'!
|
# value is in sync with value in script 'global-functions'!
|
||||||
:global GlobalConfigVersion 64;
|
:global GlobalConfigVersion 65;
|
||||||
|
|
||||||
# This is used for DNS and backup file.
|
# This is used for DNS and backup file.
|
||||||
:global Domain "example.com";
|
:global Domain "example.com";
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
# Make sure all configuration properties are up to date and this
|
# Make sure all configuration properties are up to date and this
|
||||||
# value is in sync with value in script 'global-functions'!
|
# value is in sync with value in script 'global-functions'!
|
||||||
# Comment or remove to disable news and change notifications.
|
# Comment or remove to disable news and change notifications.
|
||||||
:global GlobalConfigVersion 64;
|
:global GlobalConfigVersion 65;
|
||||||
|
|
||||||
# Copy configuration from global-config here and modify it.
|
# Copy configuration from global-config here and modify it.
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,7 @@
|
||||||
62="Added '\$ScriptRunOnce' to run a script from URL once without installation, intended to aid configuration management and the like.";
|
62="Added '\$ScriptRunOnce' to run a script from URL once without installation, intended to aid configuration management and the like.";
|
||||||
63="Moved optional functions '\$IPCalc' and '\$ScriptRunOnce' to modules.";
|
63="Moved optional functions '\$IPCalc' and '\$ScriptRunOnce' to modules.";
|
||||||
64="Implemented '\$InspectVar' in module to inspect variables.";
|
64="Implemented '\$InspectVar' in module to inspect variables.";
|
||||||
|
65="Added module to manage VLANs on bridge ports.";
|
||||||
};
|
};
|
||||||
|
|
||||||
# Migration steps to be applied on script updates
|
# Migration steps to be applied on script updates
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
# https://git.eworm.de/cgit/routeros-scripts/about/
|
# https://git.eworm.de/cgit/routeros-scripts/about/
|
||||||
|
|
||||||
# expected configuration version
|
# expected configuration version
|
||||||
:global ExpectedConfigVersion 64;
|
:global ExpectedConfigVersion 65;
|
||||||
|
|
||||||
# global variables not to be changed by user
|
# global variables not to be changed by user
|
||||||
:global GlobalFunctionsReady false;
|
:global GlobalFunctionsReady false;
|
||||||
|
|
62
global-functions.d/bridge-port-vlan
Normal file
62
global-functions.d/bridge-port-vlan
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#!rsc by RouterOS
|
||||||
|
# RouterOS script: global-functions.d/bridge-port-vlan
|
||||||
|
# Copyright (c) 2013-2021 Christian Hesse <mail@eworm.de>
|
||||||
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
||||||
|
#
|
||||||
|
# manage VLANs on bridge ports
|
||||||
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/global-functions.d/bridge-port-vlan.md
|
||||||
|
|
||||||
|
:global BridgePortVlan;
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
:global BridgePortVlan do={
|
||||||
|
:local ConfigTo [ :tostr $1 ];
|
||||||
|
|
||||||
|
:global IfThenElse;
|
||||||
|
:global LogPrintExit2;
|
||||||
|
:global ParseKeyValueStore;
|
||||||
|
|
||||||
|
:foreach BridgePort in=[ / interface bridge port find where !(comment=[]) ] do={
|
||||||
|
:local BridgePortVal [ / interface bridge port get $BridgePort ];
|
||||||
|
:foreach Config,Vlan in=[ $ParseKeyValueStore ($BridgePortVal->"comment") ] do={
|
||||||
|
:if ($Config = $ConfigTo) do={
|
||||||
|
:local DHCPClient [ / ip dhcp-client find where interface=$BridgePortVal->"interface" comment="toggle with bridge port" ];
|
||||||
|
|
||||||
|
:if ($Vlan = "dhcp-client") do={
|
||||||
|
:if ([ :len $DHCPClient ] != 1) do={
|
||||||
|
$LogPrintExit2 warning $0 ([ $IfThenElse ([ :len $DHCPClient ] = 0) "Missing" "Duplicate" ] . \
|
||||||
|
" dhcp client configuration for interface " . $BridgePortVal->"interface" . "!") true;
|
||||||
|
}
|
||||||
|
:local DHCPClientDisabled [ / ip dhcp-client get $DHCPClient disabled ];
|
||||||
|
|
||||||
|
:if ($BridgePortVal->"disabled" = false || $DHCPClientDisabled = true) do={
|
||||||
|
$LogPrintExit2 info $0 ("Disabling bridge port for interface " . $BridgePortVal->"interface" . ", enabling dhcp client.") false;
|
||||||
|
/ interface bridge port disable $BridgePort;
|
||||||
|
:delay 200ms;
|
||||||
|
/ ip dhcp-client enable $DHCPClient;
|
||||||
|
}
|
||||||
|
} else={
|
||||||
|
:if ($Vlan != [ :tostr [ :tonum $Vlan ] ]) do={
|
||||||
|
:do {
|
||||||
|
:set $Vlan ([ / interface bridge vlan get [ find where comment=$Vlan ] vlan-ids ]->0);
|
||||||
|
} on-error={
|
||||||
|
$LogPrintExit2 warning $0 ("Could not find VLAN '" . $Vlan . "' for interface " . $BridgePortVal->"interface" . "!") true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:if ($BridgePortVal->"disabled" = true || $Vlan != $BridgePortVal->"pvid") do={
|
||||||
|
$LogPrintExit2 info $0 ("Enabling bridge port for interface " . $BridgePortVal->"interface" . ", changing to " . $ConfigTo . \
|
||||||
|
" vlan " . $Vlan . ", disabling dhcp client.") false;
|
||||||
|
:if ([ :len $DHCPClient ] = 1) do={
|
||||||
|
/ ip dhcp-client disable $DHCPClient;
|
||||||
|
:delay 200ms;
|
||||||
|
}
|
||||||
|
/ interface bridge port set disabled=no pvid=$Vlan $BridgePort;
|
||||||
|
} else={
|
||||||
|
$LogPrintExit2 debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $ConfigTo . \
|
||||||
|
" vlan " . $Vlan . ".") false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue