2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: mod/bridge-port-to
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
2023-03-07 09:57:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
2024-04-04 19:45:02 +00:00
|
|
|
# requires RouterOS, version=7.13
|
|
|
|
#
|
2023-03-07 09:57:42 +00:00
|
|
|
# reset bridge ports to default bridge
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/bridge-port-to.md
|
|
|
|
|
|
|
|
:global BridgePortTo;
|
|
|
|
|
|
|
|
:set BridgePortTo do={
|
|
|
|
:local BridgePortTo [ :tostr $1 ];
|
|
|
|
|
|
|
|
:global IfThenElse;
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global ParseKeyValueStore;
|
|
|
|
|
|
|
|
:local InterfaceReEnable ({});
|
|
|
|
:foreach BridgePort in=[ /interface/bridge/port/find where !(comment=[]) ] do={
|
|
|
|
:local BridgePortVal [ /interface/bridge/port/get $BridgePort ];
|
|
|
|
:foreach Config,BridgeDefault in=[ $ParseKeyValueStore ($BridgePortVal->"comment") ] do={
|
|
|
|
:if ($Config = $BridgePortTo) do={
|
|
|
|
:local DHCPClient [ /ip/dhcp-client/find where interface=$BridgePortVal->"interface" comment="toggle with bridge port" ];
|
|
|
|
|
|
|
|
:if ($BridgeDefault = "dhcp-client") do={
|
|
|
|
:if ([ :len $DHCPClient ] != 1) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint warning $0 ([ $IfThenElse ([ :len $DHCPClient ] = 0) "Missing" "Duplicate" ] . \
|
|
|
|
" dhcp client configuration for interface " . $BridgePortVal->"interface" . "!");
|
2024-03-08 16:50:28 +00:00
|
|
|
:return false;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
:local DHCPClientDisabled [ /ip/dhcp-client/get $DHCPClient disabled ];
|
|
|
|
|
|
|
|
:if ($BridgePortVal->"disabled" = false || $DHCPClientDisabled = true) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint info $0 ("Disabling bridge port for interface " . $BridgePortVal->"interface" . ", enabling dhcp client.");
|
2023-03-07 09:57:42 +00:00
|
|
|
/interface/bridge/port/disable $BridgePort;
|
|
|
|
:delay 200ms;
|
|
|
|
/ip/dhcp-client/enable $DHCPClient;
|
|
|
|
}
|
|
|
|
} else={
|
|
|
|
:if ($BridgePortVal->"disabled" = true || $BridgeDefault != $BridgePortVal->"bridge") do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint info $0 ("Enabling bridge port for interface " . $BridgePortVal->"interface" . ", changing to " . $BridgePortTo . \
|
|
|
|
" bridge " . $BridgeDefault . ", disabling dhcp client.");
|
2023-03-07 09:57:42 +00:00
|
|
|
:if ([ :len $DHCPClient ] = 1) do={
|
|
|
|
/ip/dhcp-client/disable $DHCPClient;
|
|
|
|
:delay 200ms;
|
|
|
|
}
|
|
|
|
:local Disable [ /interface/ethernet/find where name=$BridgePortVal->"interface" ];
|
|
|
|
:if ([ :len $Disable ] > 0) do={
|
|
|
|
/interface/ethernet/disable $Disable;
|
|
|
|
:set InterfaceReEnable ($InterfaceReEnable, $Disable);
|
|
|
|
}
|
|
|
|
/interface/bridge/port/set disabled=no bridge=$BridgeDefault $BridgePort;
|
|
|
|
} else={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $BridgePortTo . \
|
|
|
|
" bridge " . $BridgeDefault . ".");
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:if ([ :len $InterfaceReEnable ] > 0) do={
|
2023-04-25 07:45:46 +00:00
|
|
|
:delay 5s;
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint info $0 ("Re-enabling interfaces...");
|
2023-03-07 09:57:42 +00:00
|
|
|
/interface/ethernet/enable $InterfaceReEnable;
|
|
|
|
}
|
|
|
|
}
|