2021-11-11 13:44:26 +00:00
|
|
|
#!rsc by RouterOS
|
2021-11-15 19:22:56 +00:00
|
|
|
# RouterOS script: mod/bridge-port-vlan
|
2022-01-01 20:38:15 +00:00
|
|
|
# Copyright (c) 2013-2022 Christian Hesse <mail@eworm.de>
|
2021-11-11 13:44:26 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# manage VLANs on bridge ports
|
2021-11-15 19:22:56 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/bridge-port-vlan.md
|
2021-11-11 13:44:26 +00:00
|
|
|
|
|
|
|
:global BridgePortVlan;
|
|
|
|
|
|
|
|
:global BridgePortVlan do={
|
|
|
|
:local ConfigTo [ :tostr $1 ];
|
|
|
|
|
2022-06-15 08:33:34 +00:00
|
|
|
:global CharacterReplace;
|
2021-11-11 13:44:26 +00:00
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global ParseKeyValueStore;
|
|
|
|
|
2022-06-15 07:59:22 +00:00
|
|
|
:local InterfaceReEnable [ :toarray "" ];
|
2022-05-10 14:09:50 +00:00
|
|
|
:foreach BridgePort in=[ /interface/bridge/port/find where !(comment=[]) ] do={
|
|
|
|
:local BridgePortVal [ /interface/bridge/port/get $BridgePort ];
|
2021-11-11 13:44:26 +00:00
|
|
|
:foreach Config,Vlan in=[ $ParseKeyValueStore ($BridgePortVal->"comment") ] do={
|
|
|
|
:if ($Config = $ConfigTo) do={
|
2022-05-10 14:09:50 +00:00
|
|
|
:local DHCPClient [ /ip/dhcp-client/find where interface=$BridgePortVal->"interface" comment="toggle with bridge port" ];
|
2021-11-11 13:44:26 +00:00
|
|
|
|
|
|
|
: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;
|
|
|
|
}
|
2022-05-10 14:09:50 +00:00
|
|
|
:local DHCPClientDisabled [ /ip/dhcp-client/get $DHCPClient disabled ];
|
2021-11-11 13:44:26 +00:00
|
|
|
|
|
|
|
:if ($BridgePortVal->"disabled" = false || $DHCPClientDisabled = true) do={
|
|
|
|
$LogPrintExit2 info $0 ("Disabling bridge port for interface " . $BridgePortVal->"interface" . ", enabling dhcp client.") false;
|
2022-05-10 14:09:50 +00:00
|
|
|
/interface/bridge/port/disable $BridgePort;
|
2021-11-11 13:44:26 +00:00
|
|
|
:delay 200ms;
|
2022-05-10 14:09:50 +00:00
|
|
|
/ip/dhcp-client/enable $DHCPClient;
|
2021-11-11 13:44:26 +00:00
|
|
|
}
|
|
|
|
} else={
|
2022-06-02 08:55:11 +00:00
|
|
|
:local VlanName $Vlan;
|
2021-11-11 13:44:26 +00:00
|
|
|
:if ($Vlan != [ :tostr [ :tonum $Vlan ] ]) do={
|
|
|
|
:do {
|
2022-05-10 14:09:50 +00:00
|
|
|
:set $Vlan ([ /interface/bridge/vlan/get [ find where comment=$Vlan ] vlan-ids ]->0);
|
2021-11-11 13:44:26 +00:00
|
|
|
} 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 . \
|
2022-06-02 08:55:11 +00:00
|
|
|
" vlan " . $Vlan . [ $IfThenElse ($Vlan != $VlanName) (" (" . $VlanName . ")") ] . ", disabling dhcp client.") false;
|
2021-11-11 13:44:26 +00:00
|
|
|
:if ([ :len $DHCPClient ] = 1) do={
|
2022-05-10 14:09:50 +00:00
|
|
|
/ip/dhcp-client/disable $DHCPClient;
|
2021-11-11 13:44:26 +00:00
|
|
|
:delay 200ms;
|
|
|
|
}
|
2022-05-16 20:27:01 +00:00
|
|
|
/interface/ethernet/disable [ find where name=$BridgePortVal->"interface" ];
|
2022-05-10 14:09:50 +00:00
|
|
|
/interface/bridge/port/set disabled=no pvid=$Vlan $BridgePort;
|
2022-06-02 08:37:31 +00:00
|
|
|
:set InterfaceReEnable ($InterfaceReEnable, $BridgePortVal->"interface");
|
2021-11-11 13:44:26 +00:00
|
|
|
} else={
|
|
|
|
$LogPrintExit2 debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $ConfigTo . \
|
|
|
|
" vlan " . $Vlan . ".") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 07:59:22 +00:00
|
|
|
}
|
|
|
|
:if ([ :len $InterfaceReEnable ] > 0) do={
|
|
|
|
:delay 2s;
|
2022-06-15 08:33:34 +00:00
|
|
|
$LogPrintExit2 info $0 ("Re-enabling interfaces: " . \
|
|
|
|
[ $CharacterReplace [ :tostr $InterfaceReEnable ] ";" " " ]) false;
|
2022-06-15 07:59:22 +00:00
|
|
|
:foreach Interface in=$InterfaceReEnable do={
|
|
|
|
/interface/ethernet/enable [ find where name=$Interface ];
|
2022-06-02 08:37:31 +00:00
|
|
|
}
|
2021-11-11 13:44:26 +00:00
|
|
|
}
|
|
|
|
}
|