From f484e45b6a1fe5284462da19c90219d0716d916a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 12 Nov 2021 14:10:13 +0100 Subject: bridge-port-to-default -> global-functions.d/bridge-port-to --- README.md | 2 +- bridge-port-to-default | 53 ------------------ bridge-port-toggle | 21 -------- doc/bridge-port.md | 86 ------------------------------ doc/global-functions.d/bridge-port-to.md | 79 +++++++++++++++++++++++++++ doc/global-functions.d/bridge-port-vlan.md | 2 +- global-config | 9 ++-- global-config-overlay | 2 +- global-config.changes | 2 + global-functions | 2 +- global-functions.d/bridge-port-to | 54 +++++++++++++++++++ 11 files changed, 142 insertions(+), 170 deletions(-) delete mode 100644 bridge-port-to-default delete mode 100644 bridge-port-toggle delete mode 100644 doc/bridge-port.md create mode 100644 doc/global-functions.d/bridge-port-to.md create mode 100644 global-functions.d/bridge-port-to diff --git a/README.md b/README.md index 1787ab5..7eb646d 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,6 @@ Available Scripts ----------------- * [Find and remove access list duplicates](doc/accesslist-duplicates.md) -* [Manage ports in bridge](doc/bridge-port.md) * [Download packages for CAP upgrade from CAPsMAN](doc/capsman-download-packages.md) * [Run rolling CAP upgrades from CAPsMAN](doc/capsman-rolling-upgrade.md) * [Renew locally issued certificates](doc/certificate-renew-issued.md) @@ -204,6 +203,7 @@ Available Scripts Available modules ----------------- +* [Manage ports in bridge](doc/global-functions.d/bridge-port-to.md) * [Manage VLANs on bridge ports](doc/global-functions.d/bridge-port-vlan.md) Contact diff --git a/bridge-port-to-default b/bridge-port-to-default deleted file mode 100644 index b8503df..0000000 --- a/bridge-port-to-default +++ /dev/null @@ -1,53 +0,0 @@ -#!rsc by RouterOS -# RouterOS script: bridge-port-to-default -# Copyright (c) 2013-2021 Christian Hesse -# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md -# -# reset bridge ports to default bridge -# https://git.eworm.de/cgit/routeros-scripts/about/doc/bridge-port.md - -:local 0 "bridge-port-to-default"; -:global GlobalFunctionsReady; -:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } - -:global BridgePortTo; - -: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,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={ - $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; - / ip dhcp-client enable $DHCPClient; - } - } 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={ - / ip dhcp-client disable $DHCPClient; - :delay 200ms; - } - / interface bridge port set disabled=no bridge=$BridgeDefault $BridgePort; - } else={ - $LogPrintExit2 debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $BridgePortTo . \ - " bridge " . $BridgeDefault . ".") false; - } - } - } - } -} diff --git a/bridge-port-toggle b/bridge-port-toggle deleted file mode 100644 index 9eeab35..0000000 --- a/bridge-port-toggle +++ /dev/null @@ -1,21 +0,0 @@ -#!rsc by RouterOS -# RouterOS script: bridge-port-toggle -# Copyright (c) 2013-2021 Christian Hesse -# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md -# -# toggle bridge ports between default and alt bridge -# https://git.eworm.de/cgit/routeros-scripts/about/doc/bridge-port.md - -:local 0 "bridge-port-toggle"; -:global GlobalFunctionsReady; -:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } - -:global BridgePortTo; - -:if ($BridgePortTo != "default") do={ - :set BridgePortTo "default"; -} else={ - :set BridgePortTo "alt"; -} - -/ system script run bridge-port-to-default; diff --git a/doc/bridge-port.md b/doc/bridge-port.md deleted file mode 100644 index 924bd6a..0000000 --- a/doc/bridge-port.md +++ /dev/null @@ -1,86 +0,0 @@ -Manage ports in bridge -====================== - -[◀ Go back to main README](../README.md) - -🛈 This script can not be used on its own but requires the base installation. -See [main README](../README.md) for details. - -Description ------------ - -These scripts are supposed to handle interfaces and switching them from -one bridge to another. - -Requirements and installation ------------------------------ - -Just install the scripts: - - $ScriptInstallUpdate bridge-port-to-default,bridge-port-toggle; - -Configuration -------------- - -The configuration goes to ports' comments (`/ interface bridge port`). - - / interface bridge port add bridge=br-guest comment="default=dhcp-client, alt=br-guest" disabled=yes interface=en1; - / interface bridge port add bridge=br-intern comment="default=br-intern, alt=br-guest" interface=en2; - / interface bridge port add bridge=br-guest comment="default=br-guest, extra=br-extra" interface=en3; - -Also dhcp client can be handled: - - / ip dhcp-client add comment="toggle with bridge port" disabled=no interface=en1; - -There is also global configuration: - -* `BridgePortTo`: specify the configuration to be applied by default - -Add a scheduler to start with default setup on system startup: - - / system scheduler add name=bridge-port-to-default on-event="/ system script run bridge-port-to-default;" start-time=startup; - -Usage and invocation --------------------- - -The usage examples show what happens with the configuration from above. - -Running the script `bridge-port-to-default` applies all configuration given -with `default=`: - - / system script run bridge-port-to-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. -* Interface `en2` is put in bridge `br-intern`. -* Interface `en3` is put in bridge `br-guest`. - -Running the script `bridge-port-toggle` toggles to configuration given -with `alt=`: - - / system script run bridge-port-toggle; - -* Interface `en1` is put in bridge `br-guest`, dhcp client for the interface is disabled. -* Interface `en2` is put in bridge `br-guest`. -* Interface `en3` is unchanged, stays in bridge `br-guest`. - -Running the script `bridge-port-toggle` again toggles back to configuration -given with `default=`. - -More configuration can be loaded by setting `BridgePortTo`: - - :set BridgePortTo "extra"; - / system script run bridge-port-to-default; - -* Interfaces `en1` and `en2` are unchanged. -* 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 top](#top) diff --git a/doc/global-functions.d/bridge-port-to.md b/doc/global-functions.d/bridge-port-to.md new file mode 100644 index 0000000..e0eba1e --- /dev/null +++ b/doc/global-functions.d/bridge-port-to.md @@ -0,0 +1,79 @@ +Manage ports in bridge +====================== + +[◀ 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 functio are are supposed to handle interfaces and +switching them from one bridge to another. + +Requirements and installation +----------------------------- + +Just install the module: + + $ScriptInstallUpdate global-functions.d/bridge-port-to; + +Configuration +------------- + +The configuration goes to ports' comments (`/ interface bridge port`). + + / interface bridge port add bridge=br-guest comment="default=dhcp-client, alt=br-guest" disabled=yes interface=en1; + / interface bridge port add bridge=br-intern comment="default=br-intern, alt=br-guest" interface=en2; + / interface bridge port add bridge=br-guest comment="default=br-guest, extra=br-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-to on-event=":global GlobalFunctionsReady; :while (\$GlobalFunctionsReady != true) do={ :delay 500ms; }; :global BridgePortTo; \$BridgePortTo default;" start-time=startup; + +Usage and invocation +-------------------- + +The usage examples show what happens with the configuration from above. + +Running the function `$BridgePortTo` with parameter `default` applies all +configuration given with `default=`: + + $BridgePortTo 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. +* Interface `en2` is put in bridge `br-intern`. +* Interface `en3` is put in bridge `br-guest`. + +Running the function `$BridgePortTo` with parameter `alt` applies all +configuration given with `alt=`: + + $BridgePortTo alt; + +* Interface `en1` is put in bridge `br-guest`, dhcp client for the interface is disabled. +* Interface `en2` is put in bridge `br-guest`. +* Interface `en3` is unchanged, stays in bridge `br-guest`. + +Running the function `$BridgePortTo` with parameter `extra` applies another +configuration: + + $BridgePortTo extra; + +* Interfaces `en1` and `en2` are unchanged. +* Interface `en3` is put in bridge `br-intern`. + +See also +-------- + +* [Manage VLANs on bridge ports](bridge-port-vlan.md) + +--- +[◀ Go back to main README](../README.md) +[▲ Go back to top](#top) diff --git a/doc/global-functions.d/bridge-port-vlan.md b/doc/global-functions.d/bridge-port-vlan.md index f59cb83..9df46b0 100644 --- a/doc/global-functions.d/bridge-port-vlan.md +++ b/doc/global-functions.d/bridge-port-vlan.md @@ -76,7 +76,7 @@ configuration: See also -------- -* [Manage ports in bridge](../bridge-port.md) +* [Manage ports in bridge](bridge-port-to.md) --- [◀ Go back to main README](../../README.md) diff --git a/global-config b/global-config index 5dc9a20..3905c8e 100644 --- a/global-config +++ b/global-config @@ -8,7 +8,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! -:global GlobalConfigVersion 65; +:global GlobalConfigVersion 66; # This is used for DNS and backup file. :global Domain "example.com"; @@ -107,9 +107,6 @@ :global CheckHealthTemperatureDeviation 2; :global CheckHealthVoltagePercent 10; -# This controls what configuration is activated by bridge-port-to-default. -:global BridgePortTo "default"; - # Access-list entries matching this comment are updated # with daily pseudo-random PSK. :global DailyPskMatchComment "Daily PSK"; @@ -134,7 +131,7 @@ 2=":global SendNotification; :global Identity; \$SendNotification (\"Hello...\") (\"Hello world, \" . \$Identity . \" calling!\");"; 3="/ system shutdown;"; 4="/ system reboot;"; - 5="/ system script run bridge-port-toggle;"; + 5=":global BridgePortVlan; \$BridgePortVlan alt;"; # add more here... }; # This led gives visual feedback if type is 'on' or 'off'. @@ -142,7 +139,7 @@ # Run commands on SMS action. :global SmsAction { - bridge-port-toggle="/ system script run bridge-port-toggle;"; + bridge-port-vlan-alt=":global BridgePortVlan; \$BridgePortVlan alt;"; reboot="/ system reboot;"; shutdown="/ system shutdown;"; # add more here... diff --git a/global-config-overlay b/global-config-overlay index 7bdc516..1570461 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -8,7 +8,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! # Comment or remove to disable news and change notifications. -:global GlobalConfigVersion 65; +:global GlobalConfigVersion 66; # Copy configuration from global-config here and modify it. diff --git a/global-config.changes b/global-config.changes index 6a8b145..2561506 100644 --- a/global-config.changes +++ b/global-config.changes @@ -69,6 +69,7 @@ 63="Moved optional functions '\$IPCalc' and '\$ScriptRunOnce' to modules."; 64="Implemented '\$InspectVar' in module to inspect variables."; 65="Added module to manage VLANs on bridge ports."; + 66="Moved script 'bridge-port-to-default' to new module."; }; # Migration steps to be applied on script updates @@ -78,4 +79,5 @@ 52=":global CertificateDownload; :if ([ :len [ / certificate find where fingerprint=\"67add1166b020ae61b8f5fc96813c04c2aa589960796865572a3c7e737613dfd\" or fingerprint=\"96bcec06264976f37460779acf28c5a7cfe8a3c0aae11a8ffcee05c0bddf08c6\" ] ] < 2) do={ \$CertificateDownload \"R3\"; }; / certificate remove [ find where fingerprint=\"0687260331a72403d909f105e69bcf0d32e1bd2493ffc6d9206d11bcd6770739\" ];"; 54=":global ScriptInstallUpdate; :global TelegramTokenId; :global TelegramChatId; :if ([ :len \$TelegramTokenId ] > 0 && [ :len \$TelegramChatId ] > 0) do={ \$ScriptInstallUpdate global-functions.d/notification-telegram; }"; 61="/ system script remove [ find where name~\"^(early-errors|global-wait|mode-button-(event|scheduler)|script-updates)\\\$\" source~\"^#!rsc by RouterOS\\n\" ];"; + 66=":global ScriptInstallUpdate; :if ([ :len [ / system script find where name=\"bridge-port-to-default\" ] ] > 0) do={ / system script remove [ find where name~\"^bridge-port-to(-default|ggle)\\\$\" ]; \$ScriptInstallUpdate global-functions.d/bridge-port-to; }"; }; diff --git a/global-functions b/global-functions index dbfeaa1..813e4b4 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 65; +:global ExpectedConfigVersion 66; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/global-functions.d/bridge-port-to b/global-functions.d/bridge-port-to new file mode 100644 index 0000000..437cba7 --- /dev/null +++ b/global-functions.d/bridge-port-to @@ -0,0 +1,54 @@ +#!rsc by RouterOS +# RouterOS script: global-functions.d/bridge-port-to +# Copyright (c) 2013-2021 Christian Hesse +# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md +# +# reset bridge ports to default bridge +# https://git.eworm.de/cgit/routeros-scripts/about/doc/global-functions.d/bridge-port-to.md + +:global BridgePortTo; + +:set BridgePortTo do={ + :local BridgePortTo [ :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,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={ + $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 ($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={ + / ip dhcp-client disable $DHCPClient; + :delay 200ms; + } + / interface bridge port set disabled=no bridge=$BridgeDefault $BridgePort; + } else={ + $LogPrintExit2 debug $0 ("Interface " . $BridgePortVal->"interface" . " already connected to " . $BridgePortTo . \ + " bridge " . $BridgeDefault . ".") false; + } + } + } + } + } +} -- cgit v1.2.3-70-g09d2