Saludos a todos por la fiesta pasada. Este tema es una versión mejorada de lo que escribí en 2016 aquí. .
Subimos el script a Mikrotik, cambiamos BotID y ChatID por los nuestros y creamos un schedule para él. Establecemos el parámetro «Start Time» en startup (Ejecutar el script al inicio).
«Intervalo»: 00:00:00
Por lo demás, todo sigue igual.
Telegram-v2
:delay 10 :global mtIdentity [ /system identity get name]; :global botID "botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX" ; :global myChatID "YYYYYY" ; :local chatId 0; :local messageId 0;:local parse do={ :local startLoc ([:find $content $variable -1] + [:len $variable] + 2); :local commaLoc ([:find $content "," $startLoc] - 1 + 1); :local braceLoc ([:find $content "}" $startLoc] - 1 + 1); :local endLoc $commaLoc; :local startSymbol [:pick $content $startLoc] :if ($braceLoc != 0 and ($commaLoc = 0 or $braceLoc < $commaLoc)) do={ :set endLoc $braceLoc; }; :if ($startSymbol = "{") do={ :set endLoc ($braceLoc + 1); }; :if ($quotas = true) do={ :set startLoc ($startLoc + 1); :set endLoc ($endLoc - 1); } :if ($endLoc 30) do={ :set messageId ([$parse content=$content variable="update_id"] + 1) :local message [$parse content=$content variable="text" quotas=true] :local chat [$parse content=$content variable="chat"] :local chatId [$parse content=$chat variable="id"] :if (($chatId = $myChatID) and [/system script find name=$message] != "") do={ /system script run $message; } else={ :tool fetch url=("https://api.telegram.org/$botID/sendmessage?chat_id=$chatId&text=$mtIdentity: Comando desconocido: $message") keep-result=no } } } on-error={} };
Después de unos años de uso, se identificó un bug, por razones desconocidas, los Mikrotik más débiles detienen el script, mientras que en los más potentes funciona sin interrupciones.Para estos fines, he creado un parche llamado WatchDog. Aquí cambiamos el nombre del script por el que indicamos arriba y establecemos un intervalo de reinicio de 5 minutos. Cada 5 minutos, nuestro «perro guardián» verificará el script, y si no está funcionando, lo iniciará.
WatchDogT.me
:global scriptname "t.me" :if ([:len [/system script job find script="$scriptname"]] > 0) do={ :log info "$scriptname Ya está en ejecución - eliminando script antiguo antes de continuar" :foreach counter in=[/system script job find script="$scriptname"] do={ /system script job remove $counter } } /system script run $scriptname
Y como postre, el script fue tomado de un foro de Mikrotik.Envía temas importantes del registro a nuestro Telegram.
Envía temas importantes del registro a nuestro Telegram.
Subimos el script al schedule y especificamos el intervalo de reinicio cada 5 minutos, cambiamos BotID y ChatID por los nuestros.
Notify-log
:global lastTime
:global output
:global mtIdentity [\/system identity get name];
:global botID "botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX" ;
:global myChatID "YYYYYY" ;
:local LogGet [ :toarray [ \/log find topics~"critical" || message~"login failure" || message~"[Ff]ailure" ] ] ;
:local LogtLineCount [ :len $LogGet ] ;
if ($LogtLineCount > 0) do={
:local currentTime "$[ \/log get [ :pick $LogGet ($LogtLineCount -1) ] time ]";
:if ([:len $currentTime] = 10 ) do={
:set currentTime [ :pick $currentTime 0 10 ];
}
:set output "$currentTime - $[\/log get [ :pick $LogGet ($LogtLineCount-1) ] message ]";
:if (([:len $lastTime] 0) && ($lastTime != $currentTime))) do={
:set lastTime $currentTime ;
:tool fetch url=("https://api.telegram.org/$botID/sendmessage?chat_id=$myChatID&text="$mtIdentity" : $output") keep-result=no
}
}Obtendremos el resultado:

Fuente: habr.com
