๋ชจ๋๋ค ๋ฆ์ ์ฐํด ์ ๋ณด๋ด์ธ์. ์ด ํ
๋ง๊ฐ ๋ ์ข์
์ผ๋ฐ์ ์ผ๋ก ์๋ ์๋ฆฌ๋ ๋ณ๊ฒฝ๋์ง ์์์ผ๋ฉฐ ์ ์ผํ ์ฐจ์ด์ ์ ์ด์ ์ง์ฒด ์์ด ์ฆ์ ์๋ํ๋ค๋ ์ ์ ๋๋ค.
์คํฌ๋ฆฝํธ๋ฅผ 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
}
}
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ถ์ฒ : habr.com