Merge branch 'FormatLine' into next

This commit is contained in:
Christian Hesse 2023-04-24 21:54:50 +02:00
commit 83e57e6aab
14 changed files with 120 additions and 89 deletions

View file

@ -17,6 +17,7 @@
:global Identity;
:global DeviceInfo;
:global FormatLine;
:global LogPrintExit2;
:global RandomDelay;
:global ScriptFromTerminal;
@ -47,9 +48,9 @@ $WaitFullyConnected;
subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \
message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \
[ $DeviceInfo ] . "\n\n" . \
"Name: " . $Cloud->"name" . "\n" . \
"Size: " . $Cloud->"size" . " B (" . ($Cloud->"size" / 1024) . " KiB)\n" . \
"Download key: " . $Cloud->"secret-download-key"); silent=true });
[ $FormatLine "Name" ($Cloud->"name") ] . "\n" . \
[ $FormatLine "Size" ($Cloud->"size" . " B (" . ($Cloud->"size" / 1024) . " KiB)") ] . "\n" . \
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true });
} on-error={
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \

View file

@ -22,6 +22,7 @@
:global CharacterReplace;
:global DeviceInfo;
:global FormatLine;
:global LogPrintExit2;
:global MkDir;
:global RandomDelay;
@ -91,9 +92,9 @@ $SendEMail2 ({ origin=$0; \
message=("See attached files for backup and config export for " . \
$Identity . ".\n\n" . \
[ $DeviceInfo ] . "\n\n" . \
"Backup file: " . $BackupFile . "\n" . \
"Export file: " . $ExportFile . "\n" . \
"Config file: " . $ConfigFile); \
[ $FormatLine "Backup file" $BackupFile ] . "\n" . \
[ $FormatLine "Export file" $ExportFile ] . "\n" . \
[ $FormatLine "Config file" $ConfigFile ]); \
attach=$Attach; remove-attach=true });
# wait for the mail to be sent

View file

@ -25,6 +25,7 @@
:global CharacterReplace;
:global DeviceInfo;
:global FormatLine;
:global IfThenElse;
:global LogPrintExit2;
:global MkDir;
@ -120,9 +121,9 @@ $SendNotification2 ({ origin=$0; \
([ $SymbolForNotification "floppy-disk,up-arrow" ] . "Backup & Config upload") ]; \
message=("Backup and config export upload for " . $Identity . ".\n\n" . \
[ $DeviceInfo ] . "\n\n" . \
"Backup file: " . $BackupFile . "\n" . \
"Export file: " . $ExportFile . "\n" . \
"Config file: " . $ConfigFile); silent=true });
[ $FormatLine "Backup file" $BackupFile ] . "\n" . \
[ $FormatLine "Export file" $ExportFile ] . "\n" . \
[ $FormatLine "Config file" $ConfigFile ]); silent=true });
:if ($Failed = 1) do={
:error "An error occured!";

View file

@ -76,6 +76,7 @@
:local FormatInfo do={
:local CertVal $1;
:global FormatLine;
:global IfThenElse;
:global ParseKeyValueStore;
@ -84,25 +85,15 @@
:return [ $CharacterReplace [ $CharacterReplace [ :tostr $1 ] "w" "w " ] "d" "d " ];
}
:local FormatSANs do={
:local SANs $1;
:local Return "";
:foreach SAN in=$SANs do={
:set Return ($Return . "\n " . $SAN);
}
:return $Return;
}
:return ( \
"Name: " . ($CertVal->"name") . "\n" . \
[ $IfThenElse ([ :len ($CertVal->"common-name") ] > 0) ("CommonName: " . ($CertVal->"common-name") . "\n") ] . \
[ $IfThenElse ([ :len ($CertVal->"subject-alt-name") ] > 0) ("SubjectAltNames:" . [ $FormatSANs ($CertVal->"subject-alt-name") ] . "\n") ] . \
"Private key: " . [ $IfThenElse (($CertVal->"private-key") = true) "available" "missing" ] . "\n" . \
"Fingerprint: " . ($CertVal->"fingerprint") . "\n" . \
"Issuer: " . ($CertVal->"ca") . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") . "\n" . \
"Validity: " . ($CertVal->"invalid-before") . " to " . ($CertVal->"invalid-after") . "\n" . \
"Expires in: " . [ $IfThenElse (($CertVal->"expired") = true) "expired" [ $FormatExpire ($CertVal->"expires-after") ] ]);
[ $FormatLine "Name" ($CertVal->"name") ] . "\n" . \
[ $IfThenElse ([ :len ($CertVal->"common-name") ] > 0) ([ $FormatLine "CommonName" ($CertVal->"common-name") ] . "\n") ] . \
[ $IfThenElse ([ :len ($CertVal->"subject-alt-name") ] > 0) ([ $FormatLine "SubjectAltNames" ($CertVal->"subject-alt-name") ] . "\n") ] . \
[ $FormatLine "Private key" [ $IfThenElse (($CertVal->"private-key") = true) "available" "missing" ] ] . "\n" . \
[ $FormatLine "Fingerprint" ($CertVal->"fingerprint") ] . "\n" . \
[ $FormatLine "Issuer" ($CertVal->"ca" . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN")) ] . "\n" . \
[ $FormatLine "Validity" ($CertVal->"invalid-before" . " to " . $CertVal->"invalid-after") ] . "\n" . \
[ $FormatLine "Expires in" [ $IfThenElse (($CertVal->"expired") = true) "expired" [ $FormatExpire ($CertVal->"expires-after") ] ] ]);
}
$WaitFullyConnected;

View file

@ -21,6 +21,7 @@
:global CheckHealthVoltagePercent;
:global Identity;
:global FormatLine;
:global IfThenElse;
:global LogPrintExit2;
:global ScriptLock;
@ -93,8 +94,8 @@ $ScriptLock $0;
subject=([ $SymbolForNotification ("high-voltage-sign,chart-" . [ $IfThenElse ($NumLast < \
$NumCurr) "in" "de" ] . "creasing") ] . "Health warning: " . $Name); \
message=("The " . $Name . " on " . $Identity . " jumped more than " . $CheckHealthVoltagePercent . "%.\n\n" . \
"old value: " . ($CheckHealthLast->$Name) . " V\n" . \
"new value: " . $Value . " V") });
[ $FormatLine "old value" ($CheckHealthLast->$Name . " V") ] . "\n" . \
[ $FormatLine "new value" ($Value . " V") ]) });
} else={
:if ($NumCurr <= $CheckHealthVoltageLow && $NumLast > $CheckHealthVoltageLow) do={
$SendNotification2 ({ origin=$0; \

View file

@ -23,6 +23,7 @@
:global SentLteFirmwareUpgradeNotification;
:global CharacterReplace;
:global FormatLine;
:global LogPrintExit2;
:global ScriptFromTerminal;
:global SendNotification2;
@ -71,9 +72,9 @@
subject=([ $SymbolForNotification "sparkles" ] . "LTE firmware upgrade"); \
message=("A new firmware version " . ($Firmware->"latest") . " is available for " . \
"LTE interface " . $IntName . " on " . $Identity . ".\n\n" . \
"Interface: " . [ $CharacterReplace ($Info->"manufacturer" . " " . $Info->"model") ("\"") "" ] . "\n" . \
"Installed: " . ($Firmware->"installed") . "\n" . \
"Available: " . ($Firmware->"latest")); silent=true });
[ $FormatLine "Interface" [ $CharacterReplace ($Info->"manufacturer" . " " . $Info->"model") ("\"") "" ] ] . "\n" . \
[ $FormatLine "Installed" ($Firmware->"installed") ] . "\n" . \
[ $FormatLine "Available" ($Firmware->"latest") ]); silent=true });
:set ($SentLteFirmwareUpgradeNotification->$IntName) ($Firmware->"latest");
}

View file

@ -17,6 +17,7 @@
:global Identity;
:global EitherOr;
:global FormatLine;
:global GetMacVendor;
:global LogPrintExit2;
:global ScriptLock;
@ -69,15 +70,15 @@ $ScriptLock $0 false 10;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "mobile-phone" ] . $RegVal->"mac-address" . " connected to " . $RegVal->"ssid"); \
message=("A device with unknown MAC address connected to " . $RegVal->"ssid" . " on " . $Identity . ".\n\n" . \
"Controller: " . $Identity . "\n" . \
"Interface: " . $RegVal->"interface" . "\n" . \
"SSID: " . $RegVal->"ssid" . "\n" . \
"MAC: " . $RegVal->"mac-address" . "\n" . \
"Vendor: " . $Vendor . "\n" . \
"Hostname: " . $HostName . "\n" . \
"Address: " . $Address . "\n" . \
"DNS name: " . $DnsName . "\n" . \
"Date: " . $DateTime) });
[ $FormatLine "Controller" $Identity ] . "\n" . \
[ $FormatLine "Interface" ($RegVal->"interface") ] . "\n" . \
[ $FormatLine "SSID" ($RegVal->"ssid") ] . "\n" . \
[ $FormatLine "MAC" ($RegVal->"mac-address") ] . "\n" . \
[ $FormatLine "Vendor" $Vendor ] . "\n" . \
[ $FormatLine "Hostname" $HostName ] . "\n" . \
[ $FormatLine "Address" $Address ] . "\n" . \
[ $FormatLine "DNS name" $DnsName ] . "\n" . \
[ $FormatLine "Date" $DateTime ]) });
}
} else={
$LogPrintExit2 debug $0 ("No mac address available... Ignoring.") false;

View file

@ -17,6 +17,7 @@
:global Identity;
:global EitherOr;
:global FormatLine;
:global GetMacVendor;
:global LogPrintExit2;
:global ScriptLock;
@ -70,15 +71,15 @@ $ScriptLock $0 false 10;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "mobile-phone" ] . $RegVal->"mac-address" . " connected to " . $RegVal->"ssid"); \
message=("A device with unknown MAC address connected to " . $RegVal->"ssid" . " on " . $Identity . ".\n\n" . \
"Controller: " . $Identity . "\n" . \
"Interface: " . $RegVal->"interface" . "\n" . \
"SSID: " . $RegVal->"ssid" . "\n" . \
"MAC: " . $RegVal->"mac-address" . "\n" . \
"Vendor: " . $Vendor . "\n" . \
"Hostname: " . $HostName . "\n" . \
"Address: " . $Address . "\n" . \
"DNS name: " . $DnsName . "\n" . \
"Date: " . $DateTime) });
[ $FormatLine "Controller" $Identity ] . "\n" . \
[ $FormatLine "Interface" ($RegVal->"interface") ] . "\n" . \
[ $FormatLine "SSID" ($RegVal->"ssid") ] . "\n" . \
[ $FormatLine "MAC" ($RegVal->"mac-address") ] . "\n" . \
[ $FormatLine "Vendor" $Vendor ] . "\n" . \
[ $FormatLine "Hostname" $HostName ] . "\n" . \
[ $FormatLine "Address" $Address ] . "\n" . \
[ $FormatLine "DNS name" $DnsName ] . "\n" . \
[ $FormatLine "Date" $DateTime ]) });
}
} else={
$LogPrintExit2 debug $0 ("No mac address available... Ignoring.") false;

View file

@ -18,6 +18,7 @@
:global Identity;
:global EitherOr;
:global FormatLine;
:global GetMacVendor;
:global LogPrintExit2;
:global ScriptLock;
@ -71,15 +72,15 @@ $ScriptLock $0 false 10;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "mobile-phone" ] . $RegVal->"mac-address" . " connected to " . $RegVal->"ssid"); \
message=("A device with unknown MAC address connected to " . $RegVal->"ssid" . " on " . $Identity . ".\n\n" . \
"Controller: " . $Identity . "\n" . \
"Interface: " . $RegVal->"interface" . "\n" . \
"SSID: " . $RegVal->"ssid" . "\n" . \
"MAC: " . $RegVal->"mac-address" . "\n" . \
"Vendor: " . $Vendor . "\n" . \
"Hostname: " . $HostName . "\n" . \
"Address: " . $Address . "\n" . \
"DNS name: " . $DnsName . "\n" . \
"Date: " . $DateTime) });
[ $FormatLine "Controller" $Identity ] . "\n" . \
[ $FormatLine "Interface" ($RegVal->"interface") ] . "\n" . \
[ $FormatLine "SSID" ($RegVal->"ssid") ] . "\n" . \
[ $FormatLine "MAC" ($RegVal->"mac-address") ] . "\n" . \
[ $FormatLine "Vendor" $Vendor ] . "\n" . \
[ $FormatLine "Hostname" $HostName ] . "\n" . \
[ $FormatLine "Address" $Address ] . "\n" . \
[ $FormatLine "DNS name" $DnsName ] . "\n" . \
[ $FormatLine "Date" $DateTime ]) });
}
} else={
$LogPrintExit2 debug $0 ("No mac address available... Ignoring.") false;

View file

@ -17,6 +17,7 @@
:global DailyPskQrCodeUrl;
:global Identity;
:global FormatLine;
:global LogPrintExit2;
:global SendNotification2;
:global SymbolForNotification;
@ -86,9 +87,9 @@ $WaitFullyConnected;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "calendar" ] . "daily PSK " . $Ssid); \
message=("This is the daily PSK on " . $Identity . ":\n\n" . \
"SSID: " . $Ssid . "\n" . \
"PSK: " . $NewPsk . "\n" . \
"Date: " . $Date . "\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 });
}
}

View file

@ -17,6 +17,7 @@
:global DailyPskQrCodeUrl;
:global Identity;
:global FormatLine;
:global LogPrintExit2;
:global SendNotification2;
:global SymbolForNotification;
@ -85,9 +86,9 @@ $WaitFullyConnected;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "calendar" ] . "daily PSK " . $Ssid); \
message=("This is the daily PSK on " . $Identity . ":\n\n" . \
"SSID: " . $Ssid . "\n" . \
"PSK: " . $NewPsk . "\n" . \
"Date: " . $Date . "\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 });
}
}

View file

@ -18,6 +18,7 @@
:global DailyPskQrCodeUrl;
:global Identity;
:global FormatLine;
:global LogPrintExit2;
:global SendNotification2;
:global SymbolForNotification;
@ -93,9 +94,9 @@ $WaitFullyConnected;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "calendar" ] . "daily PSK " . $Ssid); \
message=("This is the daily PSK on " . $Identity . ":\n\n" . \
"SSID: " . $Ssid . "\n" . \
"PSK: " . $NewPsk . "\n" . \
"Date: " . $Date . "\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 });
}
}

View file

@ -29,6 +29,7 @@
:global DownloadPackage;
:global EitherOr;
:global EscapeForRegEx;
:global FormatLine;
:global GetMacVendor;
:global GetRandom20CharAlNum;
:global GetRandom20CharHex;
@ -194,6 +195,7 @@
:global Identity;
:global IfThenElse;
:global FormatLine;
:local Resource [ /system/resource/get ];
:local RouterBoard;
@ -204,27 +206,27 @@
:local Update [ /system/package/update/get ];
:return ( \
"Hostname: " . $Identity . \
"\nBoard name: " . $Resource->"board-name" . \
"\nArchitecture: " . $Resource->"architecture-name" . \
[ $FormatLine "Hostname" $Identity ] . "\n" . \
[ $FormatLine "Board name" ($Resource->"board-name") ] . "\n" . \
[ $FormatLine "Architecture" ($Resource->"architecture-name") ] . "\n" . \
[ $IfThenElse ($RouterBoard->"routerboard" = true) \
("\nModel: " . $RouterBoard->"model" . \
[ $IfThenElse ([ :len ($RouterBoard->"revision") ] > 0) \
(" " . $RouterBoard->"revision") ] . \
"\nSerial number: " . $RouterBoard->"serial-number") ] . \
([ $FormatLine "Model" ($RouterBoard->"model") ] . \
[ $IfThenElse ([ :len ($RouterBoard->"revision") ] > 0) \
(" " . $RouterBoard->"revision") ] . "\n" . \
[ $FormatLine "Serial number" ($RouterBoard->"serial-number") ] . "\n") ] . \
[ $IfThenElse ([ :len ($License->"level") ] > 0) \
("\nLicense: " . $License->"level") ] . \
"\nRouterOS:" . \
"\n Channel: " . $Update->"channel" . \
"\n Installed: " . $Update->"installed-version" . \
([ $FormatLine "License" ($License->"level") ] . "\n") ] . \
"RouterOS:\n" . \
[ $FormatLine " Channel" ($Update->"channel") ] . "\n" . \
[ $FormatLine " Installed" ($Update->"installed-version") ] . "\n" . \
[ $IfThenElse ([ :typeof ($Update->"latest-version") ] != "nothing" && \
$Update->"installed-version" != $Update->"latest-version") \
("\n Available: " . $Update->"latest-version") ] . \
([ $FormatLine " Available" ($Update->"latest-version") ] . "\n") ] . \
[ $IfThenElse ($RouterBoard->"routerboard" = true && \
$RouterBoard->"current-firmware" != $RouterBoard->"upgrade-firmware") \
("\n Firmware: " . $RouterBoard->"current-firmware") ] . \
"\nRouterOS-Scripts:" . \
"\n Version: " . $ExpectedConfigVersion);
([ $FormatLine " Firmware" ($RouterBoard->"current-firmware") ] . "\n") ] . \
"RouterOS-Scripts:\n" . \
[ $FormatLine " Version" $ExpectedConfigVersion ]);
}
# convert line endings, DOS -> UNIX
@ -329,6 +331,32 @@
:return $Return;
}
# format a line for output
:set FormatLine do={
:local Key [ :tostr $1 ];
:local Values [ :toarray $2 ];
:local Indent [ :tonum $3 ];
:local Spaces " ";
:local Return "";
:global EitherOr;
:global FormatLine;
:set Indent [ $EitherOr $Indent 16 ];
:if ([ :len $Key ] > 0) do={ :set Return ($Key . ":"); }
:if ([ :len $Key ] > ($Indent - 2)) do={
:set Return ($Return . "\n" . [ :pick $Spaces 0 $Indent ] . ($Values->0));
} else={
:set Return ($Return . [ :pick $Spaces 0 ($Indent - [ :len $Return ]) ] . ($Values->0));
}
:foreach Value in=[ :pick $Values 1 [ :len $Values ] ] do={
:set Return ($Return . "\n" . [ $FormatLine "" ({$Value}) $Indent ]);
}
:return $Return;
}
# get MAC vendor
:set GetMacVendor do={
:local Mac [ :tostr $1 ];

View file

@ -13,18 +13,19 @@
:set IPCalc do={
:local Input [ :tostr $1 ];
:global FormatLine;
:global IPCalcReturn;
:global PrettyPrint;
:local Values [ $IPCalcReturn $1 ];
$PrettyPrint ( \
"Address: " . $Values->"address" . "\n" . \
"Netmask: " . $Values->"netmask" . "\n" . \
"Network: " . $Values->"network" . "\n" . \
"HostMin: " . $Values->"hostmin" . "\n" . \
"HostMax: " . $Values->"hostmax" . "\n" . \
"Broadcast: " . $Values->"broadcast");
[ $FormatLine "Address" ($Values->"address") ] . "\n" . \
[ $FormatLine "Netmask" ($Values->"netmask") ] . "\n" . \
[ $FormatLine "Network" ($Values->"network") ] . "\n" . \
[ $FormatLine "HostMin" ($Values->"hostmin") ] . "\n" . \
[ $FormatLine "HostMax" ($Values->"hostmax") ] . "\n" . \
[ $FormatLine "Broadcast" ($Values->"broadcast") ]);
}
# calculate and return netmask, network, min host, max host and broadcast