2018-10-09 11:12:08 +00:00
|
|
|
#!rsc
|
|
|
|
# RouterOS script: global-functions
|
|
|
|
# Copyright (c) 2013-2018 Christian Hesse <mail@eworm.de>
|
|
|
|
#
|
|
|
|
# global functions
|
|
|
|
|
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
|
|
|
|
# Note that subject and attachment are ignored for telegram!
|
|
|
|
: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 ];
|
|
|
|
|
|
|
|
: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";
|
|
|
|
}
|
|
|
|
}
|