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

๋ช‡ ๋…„ ๋™์•ˆ ์‚ฌ์šฉํ•œ ํ›„ ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•Œ ์ˆ˜ ์—†๋Š” ์ด์œ ๋กœ ์•ฝํ•œ Mikrotik์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ค‘์ง€ํ•˜์ง€๋งŒ ๋” ๊ฐ•๋ ฅํ•œ Mikrotik์—์„œ๋Š” ์ค‘์ง€ํ•˜์ง€ ์•Š๊ณ  ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ชฉ์ ์„ ์œ„ํ•ด ๋‚˜๋Š” WatchDog ๋ชฉ๋ฐœ์„ ๋˜์กŒ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์Šคํฌ๋ฆฝํŠธ ์ด๋ฆ„์„ ์œ„์— ํ‘œ์‹œ๋œ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์žฌ์‹œ์ž‘ ๊ฐ„๊ฒฉ์„ 5๋ถ„์œผ๋กœ ์„ค์ •ํ•˜์„ธ์š”. 5๋ถ„๋งˆ๋‹ค ์šฐ๋ฆฌ์˜ "watchdog"์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฉด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

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 ์Šคํฌ๋ฆฝํŠธ์˜ ์›๊ฒฉ ํ™œ์„ฑํ™”

์ถœ์ฒ˜ : habr.com

์ฝ”๋ฉ˜ํŠธ๋ฅผ ์ถ”๊ฐ€