2021-11-12 13:10:13 +00:00
|
|
|
#!rsc by RouterOS
|
2021-11-15 19:22:56 +00:00
|
|
|
# RouterOS script: mod/bridge-port-to
|
2022-01-01 20:38:15 +00:00
|
|
|
# Copyright (c) 2013-2022 Christian Hesse <mail@eworm.de>
|
2021-11-12 13:10:13 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# reset bridge ports to default bridge
|
2021-11-15 19:22:56 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/bridge-port-to.md
|
2021-11-12 13:10:13 +00:00
|
|
|
|
|
|
|
:global BridgePortTo;
|
|
|
|
|
|
|
|
:set BridgePortTo do={
|
|
|
|
:local BridgePortTo [ :tostr $1 ];
|
|
|
|
|
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global ParseKeyValueStore;
|
|
|
|
|
2022-07-13 09:34:25 +00:00
|
|
|
:local InterfaceReEnable ({});
|
2022-05-10 14:09:42 +00:00
|
|
|
:foreach BridgePort in=[ /interface/bridge/port/find where !(comment=[]) ] do={
|
|
|
|
:local BridgePortVal [ /interface/bridge/port/get $BridgePort ];
|
2021-11-12 13:10:13 +00:00
|
|
|
:foreach Config,BridgeDefault in=[ $ParseKeyValueStore ($BridgePortVal->"comment") ] do={
|
|
|
|
:if ($Config = $BridgePortTo) do={
|
2022-05-10 14:09:42 +00:00
|
|
|
:local DHCPClient [ /ip/dhcp-client/find where interface=$BridgePortVal->"interface" comment="toggle with bridge port" ];
|
2021-11-12 13:10:13 +00:00
|
|
|
|
|
|
|
:if ($BridgeDefault = "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:42 +00:00
|
|
|
:local DHCPClientDisabled [ /ip/dhcp-client/get $DHCPClient disabled ];
|
2021-11-12 13:10:13 +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:42 +00:00
|
|
|
/interface/bridge/port/disable $BridgePort;
|
2021-11-12 13:10:13 +00:00
|
|
|
:delay 200ms;
|
2022-05-10 14:09:42 +00:00
|
|
|
/ip/dhcp-client/enable $DHCPClient;
|
2021-11-12 13:10:13 +00:00
|
|
|
}
|
|
|
|
} else={
|
|
|
|
:if ($BridgePortVal->"disabled" = true || $BridgeDefault != $BridgePortVal->"bridge") do={
|
|
|
|
$LogPrintExit2 info $0 ("Enabling bridge port for interface " . $BridgePortVal->"interface" . ", changing to " . $BridgePortTo . \
|
|
|
|
" bridge " . $BridgeDefault . ", disabling dhcp client.") false;
|
|
|
|
:if ([ :len $DHCPClient ] = 1) do={
|
2022-05-10 14:09:42 +00:00
|
|
|
/ip/dhcp-client/disable $DHCPClient;
|
2021-11-12 13:10:13 +00:00
|
|
|
:delay 200ms;
|
|
|
|
}
|
2022-06-29 10:53:50 +00:00
|
|
|
:local Disable [ /interface/ethernet/find where name=$BridgePortVal->"interface" ];
|
|
|
|
:if ([ :len $Disable ] > 0) do={
|
|
|
|
/interface/ethernet/disable $Disable;
|
2022-06-30 06:59:27 +00:00
|
|
|
:set InterfaceReEnable ($InterfaceReEnable, $Disable);
|
2022-06-29 10:53:50 +00:00
|
|
|
}
|
2022-05-10 14:09:42 +00:00
|
|
|
/interface/bridge/port/set disabled=no bridge=$BridgeDefault $BridgePort;
|
2021-11-12 13:10:13 +00:00
|
|
|
} else={
|
|
|
|
$LogPrintExit2 debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $BridgePortTo . \
|
|
|
|
" bridge " . $BridgeDefault . ".") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 07:59:50 +00:00
|
|
|
}
|
|
|
|
:if ([ :len $InterfaceReEnable ] > 0) do={
|
|
|
|
:delay 2s;
|
2022-06-30 06:59:27 +00:00
|
|
|
$LogPrintExit2 info $0 ("Re-enabling interfaces...") false;
|
|
|
|
/interface/ethernet/enable $InterfaceReEnable;
|
2021-11-12 13:10:13 +00:00
|
|
|
}
|
|
|
|
}
|