bridge-port-to-default -> global-functions.d/bridge-port-to

This commit is contained in:
Christian Hesse 2021-11-12 14:10:13 +01:00
parent b6215ba958
commit f484e45b6a
10 changed files with 79 additions and 107 deletions

View file

@ -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

View file

@ -1,53 +0,0 @@
#!rsc by RouterOS
# RouterOS script: bridge-port-to-default
# Copyright (c) 2013-2021 Christian Hesse <mail@eworm.de>
# 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;
}
}
}
}
}

View file

@ -1,21 +0,0 @@
#!rsc by RouterOS
# RouterOS script: bridge-port-toggle
# Copyright (c) 2013-2021 Christian Hesse <mail@eworm.de>
# 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;

View file

@ -3,21 +3,21 @@ 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.
🛈 This module 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.
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 scripts:
Just install the module:
$ScriptInstallUpdate bridge-port-to-default,bridge-port-toggle;
$ScriptInstallUpdate global-functions.d/bridge-port-to;
Configuration
-------------
@ -32,23 +32,19 @@ 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;
/ 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 script `bridge-port-to-default` applies all configuration given
with `default=`:
Running the function `$BridgePortTo` with parameter `default` applies all
configuration given with `default=`:
/ system script run bridge-port-to-default;
$BridgePortTo default;
For the three interfaces we get this configuration:
@ -56,22 +52,19 @@ For the three interfaces we get this configuration:
* 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=`:
Running the function `$BridgePortTo` with parameter `alt` applies all
configuration given with `alt=`:
/ system script run bridge-port-toggle;
$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 script `bridge-port-toggle` again toggles back to configuration
given with `default=`.
Running the function `$BridgePortTo` with parameter `extra` applies another
configuration:
More configuration can be loaded by setting `BridgePortTo`:
:set BridgePortTo "extra";
/ system script run bridge-port-to-default;
$BridgePortTo extra;
* Interfaces `en1` and `en2` are unchanged.
* Interface `en3` is put in bridge `br-intern`.
@ -79,7 +72,7 @@ More configuration can be loaded by setting `BridgePortTo`:
See also
--------
* [Manage VLANs on bridge ports](global-functions.d/bridge-port-vlan.md)
* [Manage VLANs on bridge ports](bridge-port-vlan.md)
---
[◀ Go back to main README](../README.md)

View file

@ -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)

View file

@ -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...

View file

@ -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.

View file

@ -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; }";
};

View file

@ -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;

View file

@ -0,0 +1,54 @@
#!rsc by RouterOS
# RouterOS script: global-functions.d/bridge-port-to
# Copyright (c) 2013-2021 Christian Hesse <mail@eworm.de>
# 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;
}
}
}
}
}
}