Mungkin bagi kalian yang kenal dengan Mikrotik sudah tidak asing lagi dengan BOT ini, pada kesempatan kali ini saya ingin sharing ilmu tentang bagaimana cara monitoring jaringan dengan Mikrotik.
Sebenarnya untuk membuat BOT ini cukup simpel saja, disini saya juga sudah menyiapkan Script nya, jadi kalian hanya tinggal copy paste dan menyesuaikan saja.
Bahan yang perluh di sediakan
- Mikrotik (tentunnya)
- Bot Telegram (Jika belum memiliki bisa dilihat tutorial membuatnya Klik Disini)
- Segelas Kopi
Jika bahan sudah siap mari kita buat Scriptnya masuk ke Sytem > Script
1. Tambakan Script dengan nama “tg_config”
:put "tg: Load config":local config {"botAPI"="(API BOT TELEGRAM ANDA)";"defaultChatID"="(CHAT ID BOT ANDA, ID GROUPS)";"trusted"="(CHAT ID ANDA)";
2. Tambakan Script dengan nama “tg_getUpdates”
:global TGLASTMSGID:global TGLASTUPDID:local fconfig [:parse [/system script get tg_config source]]:local http [:parse [/system script get func_fetch source]]:local gkey [:parse [/system script get tg_getkey source]]:local send [:parse [/system script get tg_sendMessage source]]:local cfg [$fconfig]:local trusted [:toarray ($cfg->"trusted")]:local botID ($cfg->"botAPI"):local storage ($cfg->"storage"):local timeout ($cfg->"timeout"):put "cfg=$cfg":put "trusted=$trusted":put "botID=$botID":put "storage=$storage":put "timeout=$timeout":local file ($storage."tg_get_updates.txt"):local logfile ($storage."tg_fetch_log.txt")#get 1 message per time:local url ("https://api.telegram.org/bot".$botID."/getUpdates?timeout=$timeout&limit=1"):if ([:len $TGLASTUPDID]>0) do={:set url "$url&offset=$($TGLASTUPDID+1)"}:put "Reading updates...":local res [$http dst-path=$file url=$url resfile=$logfile]:if ($res!="success") do={:put "Error getting updates"return "Failed get updates"}:put "Finished to read updates.":local content [/file get [/file find name=$file] contents]:local msgid [$gkey key="message_id" text=$content]:if ($msgid="") do={:put "No new updates":return 0}:set TGLASTMSGID $msgid:local updid [$gkey key="update_id" text=$content]:set TGLASTUPDID $updid:local fromid [$gkey block="from" key="id" text=$content]:local username [$gkey block="from" key="username" text=$content]:local firstname [$gkey block="from" key="first_name" text=$content]:local lastname [$gkey block="from" key="last_name" text=$content]:local chatid [$gkey block="chat" key="id" text=$content]:local chattext [$gkey block="chat" key="text" text=$content]:put "message id=$msgid":put "update id=$updid":put "from id=$fromid":put "first name=$firstname":put "last name=$lastname":put "username=$username":local name "$firstname $lastname":if ([:len $name]<2) do {:set name $username}:put "in chat=$chatid":put "command=$chattext":local allowed ( [:type [:find $trusted $fromid]]!="nil" or [:type [:find $trusted $chatid]]!="nil"):if (!$allowed) do={:put "Unknown sender, keep silence":return -1}:local cmd "":local params "":local ltext [:len $chattext]:local pos [:find $chattext " "]:if ([:type $pos]="nil") do={:set cmd [:pick $chattext 1 $ltext]} else={:set cmd [:pick $chattext 1 $pos]:set params [:pick $chattext ($pos+1) $ltext]}:local pos [:find $cmd "@"]:if ([:type $pos]!="nil") do={:set cmd [:pick $cmd 0 $pos]}:put "cmd=<$cmd>":put "params=<$params>":global TGLASTCMD $cmd:put "Try to invoke external script tg_cmd_$cmd":local script [:parse [/system script get "tg_cmd_$cmd" source]]$script params=$params chatid=$chatid from=$name
3. Tambakan Script dengan nama “func_fetch”
:local res "fetchresult.txt":if ([:len $resfile]>0) do={:set res $resfile}#:put $res:local cmd "/tool fetch":if ([:len $mode]>0) do={:set cmd "$cmd mode=$mode"}:if ([:len $upload]>0) do={:set cmd "$cmd upload=$upload"}:if ([:len $user]>0) do={:set cmd "$cmd user=\"$user\""}:if ([:len $password]>0) do={:set cmd "$cmd password=\"$password\""}:if ([:len $address]>0) do={:set cmd "$cmd address=\"$address\""}:if ([:len $host]>0) do={:set cmd "$cmd host=\"$host\""}:if ([:len $"http-data"]>0) do={:set cmd "$cmd http-data=\"$"http-data"\""}:if ([:len $"http-method"]>0) do={:set cmd "$cmd http-method=\"$"http-method"\""}:if ([:len $"check-certificate"]>0) do={:set cmd "$cmd check-certificate=\"$"check-certificate"\""}:if ([:len $"src-path"]>0) do={:set cmd "$cmd src-path=\"$"src-path"\""}:if ([:len $"dst-path"]>0) do={:set cmd "$cmd dst-path=\"$"dst-path"\""}:if ([:len $ascii]>0) do={:set cmd "$cmd ascii=\"$ascii\""}:if ([:len $url]>0) do={:set cmd "$cmd url=\"$url\""}:put ">> $cmd":global FETCHRESULT:set FETCHRESULT "none":local script "\:global FETCHRESULT;\:do {\$cmd;\:set FETCHRESULT \"success\";\} on-error={\:set FETCHRESULT \"failed\";\}\":execute script=$script file=$res:local cnt 0#:put "$cnt -> $FETCHRESULT":while ($cnt<100 and $FETCHRESULT="none") do={:delay 1s:set $cnt ($cnt+1)#:put "$cnt -> $FETCHRESULT"}:local content [/file get [find name=$res] content]#:put $contentif ($content~"finished") do={:return "success"}:return $FETCHRESULT4. Tambakan Script dengan nama “tg_sendMessage”:local fconfig [:parse [/system script get tg_config source]]:local cfg [$fconfig]:local chatID ($cfg->"defaultChatID"):local botID ($cfg->"botAPI"):local storage ($cfg->"storage"):if ([:len $chat]>0) do={:set chatID $chat}:local url "https://api.telegram.org/bot$botID/sendmessage?chat_id=$chatID&text=$text":if ([:len $mode]>0) do={:set url ($url."&parse_mode=$mode")}:local file ($tgStorage."tg_get_updates.txt"):local logfile ($tgStorage."tg_fetch_log.txt")/tool fetch url=$url keep-result=no
5. Tambakan Script dengan nama “tg_cmd_hi”
:local send [:parse [/system script get tg_sendMessage source]]:put $params:put $chatid:put $from:local text "Router Id:* $[/system identity get name] * %0A\==================%0A\MENU TERSEDIA%0A\==================%0A\/cpu%0A\/hotspotactive%0A\/ping%0A\/public%0A\/PoeAdd%0A\/dialing%0A\/eHotspot%0A\/dHotspot%0A\/force"$send chat=$chatid text=$text mode="Markdown":return true6. Tambakan Script dengan nama “tg_cmd_cpu”:local send [:parse [/system script get tg_sendMessage source]]:local hotspot [:len [/ip hotspot active find]]:put $params:put $chatid:put $from:local text "Router Id:* $[/system identity get name] * %0A\Uptime: _$[/system resource get uptime]_%0A\CPU Load: _$[/system resource get cpu-load]%_%0A \RAM: _$(([/system resource get total-memory]-[/system resource get free-memory])/(1024*1024))M/$([/system resource get total-memory]/(1024*1024))M_%0A\Voltage: _$[:pick [/system health get voltage] 0 2]V_%0A\Temp: _$[ /system health get temperature]C_"$send chat=$chatid text=$text mode="Markdown":return true7. Tambakan Script dengan nama “tg_cmd_hotspotactive”:local send [:parse [/system script get tg_sendMessage source]]:local hotspot [:len [/ip hotspot active find]]:put $params:put $chatid:put $from:local text "Router Id:* $[/system identity get name] * %0A\Hotspot users: _$hotspot online_"$send chat=$chatid text=$text mode="Markdown":return true
8. Tambakan Script dengan nama “tg_cmd_ping”
:local send [:parse [/system script get tg_sendMessage source]]:put $params:put $chatid:put $from#Ping Variables:local avgRtt;:local pin:local pout:local datetime "$[/system clock get date] $[/system clock get time]"#Ping it real good/tool flood-ping 8.8.8.8 count=10 do={:if ($sent = 10) do={:set avgRtt $"avg-rtt":set pout $sent:set pin $received}}:local ploss (100 - (($pin * 100) / $pout)):local logmsg ("Ping Average for 8.8.8.8 - ".[:tostr $avgRtt]."ms - packet loss: ".[:tostr $ploss]."%"):log info $logmsg:local text "Router Id:* $[/system identity get name] * %0A\Tanggal : _$datetime_%0A\Ping : _8.8.8.8_%0A\Log : _$logmsg_"$send chat=$chatid text=$text mode="Markdown":return true
9. Tambakan Script dengan nama “tg_getkey”
:local cur 0:local lkey [:len $key]:local res "":local p:if ([:len $block]>0) do={:set p [:find $text $block $cur]:if ([:type $p]="nil") do={:return $res}:set cur ($p+[:len $block]+2)}:set p [:find $text $key $cur]:if ([:type $p]!="nil") do={:set cur ($p+lkey+2):set p [:find $text "," $cur]:if ([:type $p]!="nil") do={if ([:pick $text $cur]="\"") do={:set res [:pick $text ($cur+1) ($p-1)]} else={:set res [:pick $text $cur $p]}}}:return $res
10. Tambakan Schedule (System > Schedule)
/system script run tg_getUpdates
0 Komentar