Этот скрипт заточен для проверки сертификата HotSpot'а, но может быть легко адаптирован для других нужд.
Инициализируем функцию отправки SMS и Email.
:global sendEvent;
Эта переменная отвечает, как понятно из названия, за сколько дней до истечения срока сертификата начинать трубить.
:local attentionDays 3;
Объявляем прочие локальные переменные.
:local certDNSName; :local expAfter;
Выясняем какой сертификат будем проверять и дату, после которой он будет недействителен. Если какая-то ошибка, пишем в системный лог.
:do { :set certDNSName [/ip hotspot profile get [find default] dns-name]; :set expAfter [/certificate get [find common-name=$certDNSName] expires-after]; } on-error={ :local errMsg "An error occurred while checking the validity period of the certificate for HotSpot!"; :log warning ("$errMsg"); $sendEvent msg=$errMsg; :error "$errMsg"; }
Составляем мессаджи на будущее.
:local txtErrMsg "Certificate for \"$certDNSName\" expired!"; :local txtMsg "Less than $expAfter is left before the certificate for \"$certDNSName\" expires!";
Тело скрипта. Собственно, тут все просто и понятно.
:if ( [:typeof $expAfter] = "nil" ) do={ :log error ("$txtErrMsg"); $sendEvent msg=$txtErrMsg; } else={ :local seekWeek [:find $expAfter "w"]; :if ( [:typeof $seekWeek] = "nil" ) do={ :local seekDay [:find $expAfter "d"]; :if ( [:typeof $seekDay] = "nil" ) do={ :log error ("$txtMsg"); $sendEvent msg=$txtMsg; } else={ :local expDays [:pick $expAfter 0]; :if ( $expDays <= $attentionDays ) do={ :log warning ("$txtMsg"); $sendEvent msg=$txtMsg; } } } }
/system script
add comment="\CF\F0\EE\E2\E5\F0\EA\E0 \E8\F1\F2\E5\F7\E5\ED\E8\FF \F1\F0\EE\EA\
\E0 \E4\E5\E9\F1\F2\E2\E8\FF \F1\E5\F0\F2\E8\F4\E8\EA\E0\F2\E0 HotSpot" \
dont-require-permissions=no name=exp-cert-check owner=petya policy=\
read,write,policy,test source="# Written by Nikolay Soloshin (nikolay@solo\
shin.su) for RouterOS v6.46.3 on RB3011UiAS (arm) @ 2020.03\r\
\n\r\
\n# Functions used in the script. They are entered by the script \"variabl\
e-initialization\" when the device starts!\r\
\n:global sendEvent;\r\
\n\r\
\n# Local variables\r\
\n:local attentionDays 3;\r\
\n\r\
\n# Local variables used in the script\r\
\n:local certDNSName;\r\
\n:local expAfter;\r\
\n\r\
\n:do {\r\
\n :set certDNSName [/ip hotspot profile get [find default] dns-name];\r\
\n :set expAfter [/certificate get [find common-name=\$certDNSName] expir\
es-after];\r\
\n } on-error={\r\
\n :local errMsg \"An error occurred while checking the validity period\
\_of the certificate for HotSpot!\";\r\
\n :log warning (\"\$errMsg\");\r\
\n \$sendEvent msg=\$errMsg;\r\
\n :error \"\$errMsg\";\r\
\n }\r\
\n\r\
\n:local txtErrMsg \"Certificate for \\\"\$certDNSName\\\" expired!\";\r\
\n:local txtMsg \"Less than \$expAfter is left before the certificate for \
\\\"\$certDNSName\\\" expires!\";\r\
\n\r\
\n#\r\
\n# Script body\r\
\n#\r\
\n\r\
\n:if ( [:typeof \$expAfter] = \"nil\" ) do={\r\
\n :log error (\"\$txtErrMsg\");\r\
\n \$sendEvent msg=\$txtErrMsg;\r\
\n } else={\r\
\n :local seekWeek [:find \$expAfter \"w\"];\r\
\n :if ( [:typeof \$seekWeek] = \"nil\" ) do={\r\
\n :local seekDay [:find \$expAfter \"d\"];\r\
\n :if ( [:typeof \$seekDay] = \"nil\" ) do={\r\
\n :log error (\"\$txtMsg\");\r\
\n \$sendEvent msg=\$txtMsg;\r\
\n } else={\r\
\n :local expDays [:pick \$expAfter 0];\r\
\n :if ( \$expDays <= \$attentionDays ) do={\r\
\n :log warning (\"\$txtMsg\");\r\
\n \$sendEvent msg=\$txtMsg; }\r\
\n }\r\
\n }\r\
\n}\r\
\n\r\
\n# With love from Vladivostok."
Скрипт должен выполняться периодически. Параметры запуска смотрите в соответствующем разделе.
Суббота 09:23 Less than 3d10:52:56 is left before the certificate for "hotspot.site.ru" expires! Воскресенье 09:23 Less than 2d10:52:56 is left before the certificate for "hotspot.site.ru" expires! Позавчера 09:23 Less than 1d10:52:56 is left before the certificate for "hotspot.site.ru" expires! Вчера 09:23 Less than 10:52:56 is left before the certificate for "hotspot.site.ru" expires! Сегодня 09:23 Certificate for "hotspot.site.ru" expired!
Это минимально необходимый набор для работы скрипта.
Обсуждение