2018-10-09 11:12:08 +00:00
|
|
|
#!rsc
|
|
|
|
# RouterOS script: global-functions
|
2019-01-01 20:19:19 +00:00
|
|
|
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
|
2018-10-09 11:12:08 +00:00
|
|
|
#
|
|
|
|
# global functions
|
|
|
|
|
2019-01-03 14:36:26 +00:00
|
|
|
# expected configuration version
|
2019-01-03 15:05:54 +00:00
|
|
|
:global ExpectedConfigVersion 1;
|
|
|
|
|
|
|
|
# global variables not to be changed by user
|
|
|
|
:global "sent-routeros-update-notification" "-";
|
|
|
|
:global "sent-lte-firmware-upgrade-notification" "-";
|
|
|
|
:global "identity" [ / system identity get name ];
|
2019-01-03 14:36:26 +00:00
|
|
|
|
2018-11-09 20:13:18 +00:00
|
|
|
# read input from user
|
|
|
|
:global Read do={
|
|
|
|
:return;
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:48:54 +00:00
|
|
|
# url encoding
|
|
|
|
:global UrlEncode do={
|
2018-10-12 12:07:47 +00:00
|
|
|
:local input [ :tostr $1 ];
|
2018-10-09 14:48:54 +00:00
|
|
|
:local return "";
|
|
|
|
|
2018-10-12 12:07:47 +00:00
|
|
|
:if ([ :len $input ] > 0) do={
|
2018-10-09 14:48:54 +00:00
|
|
|
:local chars " %&";
|
|
|
|
:local subs { "%20"; "%25"; "%26" };
|
|
|
|
|
2018-10-12 12:07:47 +00:00
|
|
|
:for i from=0 to=([ :len $input ] - 1) do={
|
|
|
|
:local char [ :pick $input $i ];
|
2018-10-09 14:48:54 +00:00
|
|
|
:local replace [ :find $chars $char ];
|
|
|
|
|
|
|
|
:if ([ :len $replace ] > 0) do={
|
2018-12-12 20:57:17 +00:00
|
|
|
:set char ($subs->$replace);
|
2018-10-09 14:48:54 +00:00
|
|
|
}
|
2018-12-12 20:57:17 +00:00
|
|
|
:set return ($return . $char);
|
2018-10-09 14:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:return $return;
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:32:08 +00:00
|
|
|
# check and import required certificates
|
|
|
|
:global CertificateAvailable do={
|
2018-12-20 21:21:00 +00:00
|
|
|
:local commonname [ :tostr $1 ];
|
|
|
|
:local filename ([ :tostr $2 ] . ".pem");
|
2018-10-16 13:32:08 +00:00
|
|
|
|
|
|
|
:global "script-updates-baseurl";
|
|
|
|
:global "script-updates-urlsuffix";
|
|
|
|
|
2018-12-20 21:21:00 +00:00
|
|
|
:if ([ / certificate print count-only where common-name=$commonname ] = 0) do={
|
|
|
|
:log info ("Certificate with CommonName " . $commonname . \
|
2018-10-16 13:32:08 +00:00
|
|
|
" not available, downloading and importing.");
|
|
|
|
:do {
|
|
|
|
/ tool fetch check-certificate=yes-without-crl \
|
|
|
|
($"script-updates-baseurl" . "certs/" . \
|
2018-12-20 21:21:00 +00:00
|
|
|
$filename . $"script-updates-urlsuffix") \
|
|
|
|
dst-path=$filename;
|
|
|
|
/ certificate import file-name=$filename passphrase="";
|
2018-10-16 13:32:08 +00:00
|
|
|
} on-error={
|
|
|
|
:log warning "Failed imprting certificate!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 13:46:39 +00:00
|
|
|
# send notification via e-mail and telegram
|
2018-12-28 18:37:32 +00:00
|
|
|
# Note that attachment is ignored for telegram!
|
2018-10-09 13:46:39 +00:00
|
|
|
:global SendNotification do={
|
2018-10-12 12:07:47 +00:00
|
|
|
:local subject [ :tostr $1 ];
|
|
|
|
:local message [ :tostr $2 ];
|
|
|
|
:local attach [ :tostr $3 ];
|
2018-10-09 13:46:39 +00:00
|
|
|
|
2018-11-27 13:08:14 +00:00
|
|
|
:global "identity";
|
2018-10-09 13:46:39 +00:00
|
|
|
:global "email-general-to";
|
|
|
|
:global "email-general-cc";
|
|
|
|
:global "telegram-tokenid";
|
|
|
|
:global "telegram-chatid";
|
|
|
|
|
2018-10-09 14:48:54 +00:00
|
|
|
:global UrlEncode;
|
2018-10-16 13:32:08 +00:00
|
|
|
:global CertificateAvailable;
|
2018-10-09 14:48:54 +00:00
|
|
|
|
2018-10-09 13:46:39 +00:00
|
|
|
:if ([ :len $"email-general-to" ] > 0) do={
|
|
|
|
:do {
|
|
|
|
/ tool e-mail send to=$"email-general-to" cc=$"email-general-cc" \
|
2018-11-27 13:08:14 +00:00
|
|
|
subject=("[" . $"identity" . "] " . $subject) body=$message file=$attach;
|
2018-10-09 13:46:39 +00:00
|
|
|
} on-error={
|
|
|
|
:log warning "Failed sending notification mail!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ([ :len $"telegram-tokenid" ] > 0 && [ :len $"telegram-chatid" ] > 0) do={
|
2018-12-20 21:21:00 +00:00
|
|
|
$CertificateAvailable "Go Daddy Secure Certificate Authority - G2" "godaddy";
|
2018-10-15 07:57:13 +00:00
|
|
|
:do {
|
|
|
|
/ tool fetch check-certificate=yes-without-crl keep-result=no http-method=post \
|
2018-10-09 13:46:39 +00:00
|
|
|
("https://api.telegram.org/bot" . $"telegram-tokenid" . "/sendMessage") \
|
2018-11-27 13:08:14 +00:00
|
|
|
http-data=("chat_id=" . $"telegram-chatid" . "&text=" . \
|
|
|
|
[ $UrlEncode ("[" . $"identity" . "] " . $subject . "\n\n" . $message) ]);
|
2018-10-09 13:46:39 +00:00
|
|
|
} on-error={
|
|
|
|
:log warning "Failed sending telegram notification!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 23:48:56 +00:00
|
|
|
|
|
|
|
# get MAC vendor
|
|
|
|
:global GetMacVendor do={
|
|
|
|
:local mac [ :tostr $1 ];
|
|
|
|
|
2018-12-28 18:39:59 +00:00
|
|
|
:global CertificateAvailable;
|
|
|
|
|
2018-12-26 23:48:56 +00:00
|
|
|
:do {
|
|
|
|
:local vendor;
|
|
|
|
$CertificateAvailable "Let's Encrypt Authority X3" "letsencrypt";
|
|
|
|
:set vendor ([ / tool fetch mode=https check-certificate=yes-without-crl \
|
|
|
|
url=("https://api.macvendors.com/" . [ :pick $mac 0 8 ]) output=user as-value ]->"data");
|
|
|
|
:return $vendor;
|
|
|
|
} on-error={
|
|
|
|
:return "unknown vendor";
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 18:30:15 +00:00
|
|
|
|
|
|
|
# download package from upgrade server
|
|
|
|
:global DownloadPackage do={
|
|
|
|
:local pkgname [ :tostr $1 ];
|
|
|
|
:local pkgver [ :tostr $2 ];
|
|
|
|
:local pkgarch [ :tostr $3 ];
|
|
|
|
:local pkgdest [ :tostr $4 ];
|
|
|
|
|
|
|
|
:global CertificateAvailable;
|
|
|
|
|
|
|
|
:if ([ :len $pkgname ] = 0) do={ return false; }
|
|
|
|
:if ([ :len $pkgver ] = 0) do={ :set pkgver [ / system package update get installed-version ]; }
|
|
|
|
:if ([ :len $pkgarch ] = 0) do={ :set pkgarch [ / system resource get architecture-name ]; }
|
|
|
|
|
|
|
|
$CertificateAvailable "Let's Encrypt Authority X3" "letsencrypt";
|
|
|
|
do {
|
|
|
|
:local pkgfile ($pkgname . "-" . $pkgver . "-" . $pkgarch . ".npk");
|
|
|
|
/ tool fetch mode=https check-certificate=yes-without-crl \
|
|
|
|
("https://upgrade.mikrotik.com/routeros/" . $pkgver . "/" . $pkgfile) \
|
|
|
|
dst-path=($pkgdest . "/" . $pkgfile);
|
|
|
|
return true;
|
|
|
|
} on-error={
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|