Gníomhachtú cianda ar scripteanna Mikrotik ó Telegram v 2.0

Go léir leis an saoire atá caite. Tá an téama seo níos fearr leagan den méid a scríobh mé siar in 2016 anseo.

Go ginearálta, níl aon athrú ar phrionsabal na hoibríochta, agus is é an t-aon difríocht amháin ná go n-oibríonn sé láithreach gan mhoill.

Uaslódálann muid an script go Mikrotik, athraímid an BotID agus ChatID chuig ár gcuid féin agus cruthaímid sceideal dó. Socraigh an paraiméadar "Am Tosaigh" don am tosaithe (Rith an script ag am tosaithe.)
Eatramh: 00:00:00
Tá gach rud eile mar a bhí.

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

Ar feadh cúpla bliain úsáide, nochtadh fabht, ar chúis éigin anaithnid, Mikrotik lag ar chúis éigin stopann an script, ar cinn níos cumhachtaí oibríonn sé gan stad.

Chun na gcríoch sin, chaith mé crutch WatchDog. anseo athraíonn muid ainm na scripte go dtí an ceann atá léirithe thuas. agus socraigh an t-eatramh atosú go 5 nóiméad. Gach 5 nóiméad, déanfaidh ár "madra faire" an script a sheiceáil, agus mura n-oibríonn sé, reáchtálfar é.

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 le haghaidh milseog, tógtar an script ó fhóram Mikrotik.
Seoltar topaicí tábhachtacha ón logáil chuig ár gcairt.

Caith an script isteach sa sceideal agus sonraigh an t-eatramh atosú gach 5 nóiméad, athraigh an BotID agus ChatID chuig do chuid féin.

fógra-logáil

: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
   }
}

Faighimid an toradh:

Gníomhachtú cianda ar scripteanna Mikrotik ó Telegram v 2.0

Foinse: will.com

Add a comment