Gnìomhachadh iomallach de sgriobtaichean Mikrotik bho Telegram v 2.0

A h-uile còmhla ris na saor-làithean a dh'fhalbh. Tha an cuspair seo nas fheàrr dreach de na sgrìobh mi air ais ann an 2016 an seo.

San fharsaingeachd, chan eil am prionnsapal obrachaidh air atharrachadh, agus is e an aon eadar-dhealachadh gu bheil e a-nis ag obair sa bhad gun dàil.

Bidh sinn a’ luchdachadh suas an sgriobt gu Mikrotik, ag atharrachadh am BotID agus ChatID ris an fheadhainn againn fhìn agus a’ cruthachadh clàr air a shon. Suidhich am paramadair “Start Time” airson tòiseachadh (Ruith an sgriobt aig toiseach tòiseachaidh.)
Eadar-ama: 00:00:00
Tha a h-uile càil eile mar a bha e.

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 < $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={}
};

Airson dà bhliadhna de chleachdadh, chaidh bug fhoillseachadh, airson adhbhar air choireigin, tha Mikrotik lag airson adhbhar air choireigin a ’cur stad air an sgriobt, air feadhainn nas cumhachdaiche bidh e ag obair gun stad.

Airson na h-adhbharan sin, thilg mi crutch WatchDog. an seo atharraichidh sinn ainm an sgriobt chun an fhear a tha air a chomharrachadh gu h-àrd. agus cuir an ùine ath-thòiseachadh gu 5 mionaidean. Gach 5 mionaid, bheir an “cù faire” againn sùil air an sgriobt, agus mura obraich e, ruithidh e e.

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

Agus airson milseag, tha an sgriobt air a thoirt bhon fhòram Mikrotik.
A’ cur cuspairean cudromach bhon log chun chairt againn.

Tilg an sgriobt a-steach don chlàr agus sònraich an eadar-ama ath-thòiseachadh gach 5 mionaidean, atharraich am BotID agus ChatID riut fhèin.

fios-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] < 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
   }
}

Gheibh sinn an toradh:

Gnìomhachadh iomallach de sgriobtaichean Mikrotik bho Telegram v 2.0

Source: www.habr.com

Cuir beachd ann