Telegram v 2.0 වෙතින් Mikrotik ස්ක්‍රිප්ට් දුරස්ථව සක්‍රීය කිරීම

ප්‍රමාද වූ සැමට සුබ නිවාඩු දිනයක් වේවා. මෙම තේමාව වඩා හොඳය මම 2016 දී නැවත ලියූ දෙයෙහි අනුවාදය මෙහි.

සාමාන්යයෙන්, මෙහෙයුම් මූලධර්මය වෙනස් වී නැත, එකම වෙනස වන්නේ දැන් එය ප්රමාදයකින් තොරව ක්ෂණිකව ක්රියා කරයි.

අපි ස්ක්‍රිප්ට් එක Mikrotik වෙත උඩුගත කර, BotID සහ ChatID අපගේම ලෙස වෙනස් කර ඒ සඳහා කාලසටහනක් සාදන්නෙමු. "ආරම්භක වේලාව" පරාමිතිය ආරම්භ කිරීමට සකසන්න (ආරම්භයේදී ස්ක්‍රිප්ට් ධාවනය කරන්න.)
"ඉන්ටර්වල්": 00:00:00
එසේ නොමැතිනම් සෑම දෙයක්ම තිබූ ආකාරයටම සිදු වේ.

ටෙලිග්‍රාම්-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 < $startLoc) do={
    :set endLoc ($startLoc + 1);
  };
  :local message [:pick $content $startLoc $endLoc]
  #:log info $message;
  :return $message;
}


:while ( true ) do={
  :do {
    #:log info "https://api.telegram.org/$botID/getUpdates?offset=$messageId&limit=1&allowed_updates=message&timeout=60";
    :tool fetch url=("https://api.telegram.org/$botID/getUpdates?offset=$messageId&limit=1&allowed_updates=message&timeout=60") dst-path="getUpdates";
    :local content [/file get [/file find name=getUpdates] contents] ;
    #:log info $content;
    :if ([:len $content] > 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: Unknown command: $message") keep-result=no
      }
    }
  } on-error={}
};

වසර කිහිපයක භාවිතයෙන් පසු, දෝෂයක් සොයා ගන්නා ලදී: කිසියම් නොදන්නා හේතුවක් නිසා, දුර්වල මයික්රොටික් ස්ක්‍රිප්ට් එක නවත්වයි, නමුත් වඩා බලවත් ඒවා මත එය නතර නොවී ක්‍රියා කරයි.

මෙම අරමුණු සඳහා, මම WatchDog කිහිලිකරු මත විසි කළා. මෙහිදී අපි ස්ක්‍රිප්ට් එකේ නම ඉහත දක්වා ඇති එකට වෙනස් කරමු. සහ නැවත ආරම්භ කිරීමේ පරතරය විනාඩි 5 දක්වා සකසන්න. සෑම විනාඩි 5 කට වරක් අපගේ "මුර බල්ලා" ස්ක්‍රිප්ට් එක පරීක්ෂා කරන අතර එය ක්‍රියා නොකරන්නේ නම් එය ක්‍රියාත්මක කරයි.

WatchDogT.me

:global scriptname "t.me"
:if ([:len [/system script job find script=$"scriptname"]] > 0) do={
:log info "$scriptname Already Running - killing old script before continuing"
:foreach counter in=[/system script job find script=$"scriptname"] do={
/system script job remove $counter
}
}
/system script run $scriptname

හොඳයි, අතුරුපස සඳහා, පිටපත Mikrotik සංසදයෙන් ලබාගෙන ඇත.
ලොගයෙන් වැදගත් මාතෘකා අපගේ කරත්තයට යවයි.

අපි උපලේඛනගත කිරීමට ස්ක්‍රිප්ට් එක එකතු කර සෑම විනාඩි 5කට වරක් නැවත ආරම්ඹන කාල සීමාව සඳහන් කරන්න, BotID සහ ChatID අපේම බවට වෙනස් කරන්න.

දැනුම්දීම-ලොගය

: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] < 1) || (([: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
   }
}

අපි ප්රතිඵලය ලබා ගනිමු:

Telegram v 2.0 වෙතින් Mikrotik ස්ක්‍රිප්ට් දුරස්ථව සක්‍රීය කිරීම

මූලාශ්රය: www.habr.com

අදහස් එක් කරන්න