2020-09-18 09:00:27 +00:00
|
|
|
#!rsc by RouterOS
|
2019-03-06 14:25:18 +00:00
|
|
|
# RouterOS script: daily-psk%TEMPL%
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
2019-03-06 13:43:44 +00:00
|
|
|
# Michael Gisbers <michael@gisbers.de>
|
2020-06-19 20:17:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2018-07-05 13:29:26 +00:00
|
|
|
#
|
2023-11-15 20:31:18 +00:00
|
|
|
# requires RouterOS, version=7.12
|
|
|
|
#
|
2018-07-05 13:29:26 +00:00
|
|
|
# update daily PSK (pre shared key)
|
2020-03-27 20:44:36 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/daily-psk.md
|
2019-04-03 20:51:15 +00:00
|
|
|
#
|
2023-08-07 12:37:52 +00:00
|
|
|
# !! This is just a template to generate the real script!
|
|
|
|
# !! Pattern '%TEMPL%' is replaced, paths are filtered.
|
2018-07-05 13:29:26 +00:00
|
|
|
|
2021-02-18 13:52:47 +00:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2024-03-06 14:28:55 +00:00
|
|
|
:do {
|
|
|
|
:local ScriptName [ :jobname ];
|
2018-07-05 13:29:26 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:global DailyPskMatchComment;
|
|
|
|
:global DailyPskQrCodeUrl;
|
|
|
|
:global Identity;
|
2018-10-10 19:47:33 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:global FormatLine;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global ScriptLock;
|
|
|
|
:global SendNotification2;
|
|
|
|
:global SymbolForNotification;
|
|
|
|
:global UrlEncode;
|
|
|
|
:global WaitForFile;
|
|
|
|
:global WaitFullyConnected;
|
2018-10-10 19:47:33 +00:00
|
|
|
|
2024-03-05 15:12:36 +00:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-03-06 14:28:55 +00:00
|
|
|
:error false;
|
2024-03-05 15:12:36 +00:00
|
|
|
}
|
2024-03-04 12:48:00 +00:00
|
|
|
$WaitFullyConnected;
|
2018-10-10 18:45:00 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
# return pseudo-random string for PSK
|
|
|
|
:local GeneratePSK do={
|
|
|
|
:local Date [ :tostr $1 ];
|
2018-10-10 18:45:00 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:global DailyPskSecrets;
|
2018-10-10 18:45:00 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:global ParseDate;
|
2018-10-10 18:45:00 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:set Date [ $ParseDate $Date ];
|
global: variable names are CamelCase
___ _ ___ __
/ _ )(_)__ _ / _/__ _/ /_
/ _ / / _ `/ / _/ _ `/ __/
/____/_/\_, / /_/ \_,_/\__/
_ __ /___/ _ __
| | / /___ __________ (_)___ ____ _/ /
| | /| / / __ `/ ___/ __ \/ / __ \/ __ `/ /
| |/ |/ / /_/ / / / / / / / / / / /_/ /_/
|__/|__/\__,_/_/ /_/ /_/_/_/ /_/\__, (_)
/____/
RouterOS has some odd behavior when it comes to variable names. Let's
have a look at the interfaces:
[admin@MikroTik] > / interface print where name=en1
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
That looks ok. Now we use a script:
{ :local interface "en1";
/ interface print where name=$interface; }
And the result...
[admin@MikroTik] > { :local interface "en1";
{... / interface print where name=$interface; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
... still looks ok.
We make a little modification to the script:
{ :local name "en1";
/ interface print where name=$name; }
And the result:
[admin@MikroTik] > { :local name "en1";
{... / interface print where name=$name; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
0 RS en1 ether 1500 1598
1 S en2 ether 1500 1598
2 S en3 ether 1500 1598
3 S en4 ether 1500 1598
4 S en5 ether 1500 1598
5 R br-local bridge 1500 1598
Ups! The filter has no effect!
That happens whenever the variable name ($name) matches the property
name (name=).
And another modification:
{ :local type "en1";
/ interface print where name=$type; }
And the result:
[admin@MikroTik] > { :local type "en1";
{... / interface print where name=$type; }
Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU
Ups! Nothing?
Even if the variable name ($type) matches whatever property name (type=)
things go wrong.
The answer from MikroTik support (in Ticket#2019010222000454):
> This is how scripting works in RouterOS and we will not fix it.
To get around this we use variable names in CamelCase. Let's hope
Mikrotik never ever introduces property names in CamelCase...
*fingers crossed*
2019-01-03 16:45:43 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:local A ((14 - ($Date->"month")) / 12);
|
|
|
|
:local B (($Date->"year") - $A);
|
|
|
|
:local C (($Date->"month") + 12 * $A - 2);
|
|
|
|
:local WeekDay (7000 + ($Date->"day") + $B + ($B / 4) - ($B / 100) + ($B / 400) + ((31 * $C) / 12));
|
|
|
|
:set WeekDay ($WeekDay - (($WeekDay / 7) * 7));
|
|
|
|
|
|
|
|
:return (($DailyPskSecrets->0->(($Date->"day") - 1)) . \
|
|
|
|
($DailyPskSecrets->1->(($Date->"month") - 1)) . \
|
|
|
|
($DailyPskSecrets->2->$WeekDay));
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Seen ({});
|
|
|
|
:local Date [ /system/clock/get date ];
|
|
|
|
:local NewPsk [ $GeneratePSK $Date ];
|
2018-10-09 11:32:45 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:foreach AccList in=[ /caps-man/access-list/find where comment~$DailyPskMatchComment ] do={
|
|
|
|
:foreach AccList in=[ /interface/wifi/access-list/find where comment~$DailyPskMatchComment ] do={
|
|
|
|
:foreach AccList in=[ /interface/wireless/access-list/find where comment~$DailyPskMatchComment ] do={
|
|
|
|
:local SsidRegExp [ /caps-man/access-list/get $AccList ssid-regexp ];
|
|
|
|
:local SsidRegExp [ /interface/wifi/access-list/get $AccList ssid-regexp ];
|
|
|
|
:local Configuration ([ /caps-man/configuration/find where ssid~$SsidRegExp ]->0);
|
|
|
|
:local Configuration ([ /interface/wifi/configuration/find where ssid~$SsidRegExp ]->0);
|
|
|
|
:local Ssid [ /caps-man/configuration/get $Configuration ssid ];
|
|
|
|
:local Ssid [ /interface/wifi/configuration/get $Configuration ssid ];
|
|
|
|
:local OldPsk [ /caps-man/access-list/get $AccList private-passphrase ];
|
|
|
|
:local OldPsk [ /interface/wifi/access-list/get $AccList passphrase ];
|
2024-03-05 10:10:24 +00:00
|
|
|
# /caps-man/ /interface/wifi/ above - /interface/wireless/ below
|
2024-03-04 12:48:00 +00:00
|
|
|
:local IntName [ /interface/wireless/access-list/get $AccList interface ];
|
|
|
|
:local Ssid [ /interface/wireless/get $IntName ssid ];
|
|
|
|
:local OldPsk [ /interface/wireless/access-list/get $AccList private-pre-shared-key ];
|
|
|
|
:local Skip 0;
|
|
|
|
|
|
|
|
:if ($NewPsk != $OldPsk) do={
|
|
|
|
$LogPrintExit2 info $ScriptName ("Updating daily PSK for " . $Ssid . " to " . $NewPsk . " (was " . $OldPsk . ")") false;
|
|
|
|
/caps-man/access-list/set $AccList private-passphrase=$NewPsk;
|
|
|
|
/interface/wifi/access-list/set $AccList passphrase=$NewPsk;
|
|
|
|
/interface/wireless/access-list/set $AccList private-pre-shared-key=$NewPsk;
|
|
|
|
|
|
|
|
:if ([ :len [ /caps-man/actual-interface-configuration/find where configuration.ssid=$Ssid !disabled ] ] > 0) do={
|
|
|
|
:if ([ :len [ /interface/wifi/actual-configuration/find where configuration.ssid=$Ssid ] ] > 0) do={
|
|
|
|
:if ([ :len [ /interface/wireless/find where name=$IntName !disabled ] ] = 1) do={
|
|
|
|
:if ($Seen->$Ssid = 1) do={
|
|
|
|
$LogPrintExit2 debug $ScriptName ("Already sent a mail for SSID " . $Ssid . ", skipping.") false;
|
|
|
|
} else={
|
|
|
|
:local Link ($DailyPskQrCodeUrl . \
|
|
|
|
"?scale=8&level=1&ssid=" . [ $UrlEncode $Ssid ] . "&pass=" . [ $UrlEncode $NewPsk ]);
|
|
|
|
$SendNotification2 ({ origin=$ScriptName; \
|
|
|
|
subject=([ $SymbolForNotification "calendar" ] . "daily PSK " . $Ssid); \
|
|
|
|
message=("This is the daily PSK on " . $Identity . ":\n\n" . \
|
|
|
|
[ $FormatLine "SSID" $Ssid ] . "\n" . \
|
|
|
|
[ $FormatLine "PSK" $NewPsk ] . "\n" . \
|
|
|
|
[ $FormatLine "Date" $Date ] . "\n\n" . \
|
|
|
|
"A client device specific rule must not exist!"); link=$Link });
|
|
|
|
:set ($Seen->$Ssid) 1;
|
|
|
|
}
|
2018-10-10 19:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 13:29:26 +00:00
|
|
|
}
|
2024-03-06 14:28:55 +00:00
|
|
|
} on-error={ }
|