Mikrotik PHP
Mikrotik PHP
php
//=====================================================PLEASE NOT TO BE
DELETED====================================================//
/*
* Base Code : Banghasan
* moded : BangAchil
* Email : [email protected]
* Telegram : @bangachil
*
* Name : Mikrotik bot telegram - php
* Fungsi : Monitoring mikortik api (Edit Rule Comingsoon )
* Pembuatan : November 2018
* version : 3.1.0 last 1.0.0, 1.2.0, 1.3.0, 3.0.0
* Thnks to Banghasan
* ____________________________________________________________
*/
//=====================================================PLEASE NOT TO BE
DELETED====================================================//
/*
Command avalibe update time
* /address
* /pool
* /ping
* /Dhcp
* /monitor
* /traffic
* /dns
* /hotspot (aktif)(user)
* /resource
* /interface or (bride)
*NEW
* /neighbor
* /ipbinding
* +user
* /remove user
* other comingsoon
*/
/**********************************************************************************
**
* ** methode long poolling** *
* Perisapkan Sebuah PC atau sebuah vps
* OS windows Linux other
* Internet
* InstalL Apliaksi WEBSERVER (OS WINSOWS XAMPP, APPSERV )
* Copy file zip ini didalam sebuah folder root www/htdocs ()
* extrack file
* edit file data.json dengan notepad++ (recom) atau notepade
* edit iprouter username dan pasword
* Kemudian simpan
* edit file mikrotik.php
* edit token bot dan username bot
* Kemudian simpan
* Anda bisa langsung menjalankan bot
* dengan cara menggunakan CMD bukan membukanya melalui webbrowser
* Langkah - Langkah Running bot
* * Masuk ke tempat file mikbotam berada
* * tekan CTRL + klik kanan maouse
* * Kemudian sort cousor ke Open command window here
* * Muncul window CMD
* * Run bot dengan Mengetikan php mikrotik.php Kemudian Enter atau $
mikrotik.php
* * Jika anda melihat sebuah text
* FrameBot version 1.5
* Mode : Long Polling
* Debug : ON
* * Selamat Bot anda berjalan
* jika error pastikan komputer terhubung ke internet dan dapat melakukan ping ke
mikrotik
* Edit file mikrotik.php sesuai Kebutuhan anda happy coding
*
*****************************************************************************/
/*
* ** methode webhook hosting ** *
Persiapan Webhook
Contoh :
https://api.telegram.org/botsHgGbgHhTRdCcDFfFDcFfdEwWsXcvVBhJujYt/setWebhook?
url=https://mywebpagetorespondtobot/mikrotik.php .
require_once 'src/FrameBot.php';
$bot = new FrameBot('TOKEN_BOT', 'BOT_USERNAME'); //Ganti sesuai dengan token dan
username bot
require_once ('formatbytesbites.php');
require_once ('routeros_api.class.php');
if($dhcp=='lease'){
$getlease = $API->comm("/ip/dhcp-server/lease/print");
$TotalReg = count($getlease);
$countlease = $API->comm("/ip/dhcp-server/lease/print", array(
"count-only" => "",
));
if ($countlease < 2) {
echo "$countlease item";
}
else if ($countlease > 1) {
$data1.= "$countlease items";
};
$data.= "<b> 🏷 Daftar DHCP Total : $TotalReg </b>\n\n";
for ($i = 0; $i < $TotalReg; $i++) {
$lease = $getlease[$i];
$id = $lease['.id'];
$addr = $lease['address'];
$maca = $lease['mac-address'];
$server = $lease['server'];
$aaddr = $lease['active-address'];
$amaca = $lease['active-mac-address'];
$ahostname = $lease['host-name'];
$host = str_replace("android", "AD", $ahostname);
$status = $lease['status'];
if ($lease['dynamic'] == "true") {
$dy = "🎯Dynamic";
}
else {
$dy = "📝Static";
}
$data.= "🔎 Dhcp to $addr \n ";
$data.= "┠ <code>$dy</code> \n";
$data.= " ┠ <b>IP</b> : <code>$addr</code>\n";
$data.= " ┠ <b>Mac</b> : <code>$maca</code>\n";
$data.= " ┠ <b>DHCP</b> : <code>$server</code>\n";
$data.= " ┗ <b>HOST</b> : <code>$host</code>\n";
$data.= "\n ";
}
}else if($dhcp=='server'){
$ARRAY = $API->comm("/ip/dhcp-server/print");
$datatext.= "DHCP SERVER LIST\n\n";
//kumpulkan data
$num = count($ARRAY);
for ($i = 0; $i < $num; $i++) {
$name = $ARRAY[$i]['name'];
$interface = $ARRAY[$i]['interface'];
$lease = $ARRAY[$i]['lease-time'];
$bootp = $ARRAY[$i]['bootp-support'];
$authoritative = $ARRAY[$i]['authoritative'];
$use_radius = $ARRAY[$i]['use-radius'];
$dynamic = $ARRAY[$i]['dynamic'];
$disable = $ARRAY[$i]['disabled'];
$data.= "\n";
$data.= "📋 Dhcp Server\n";
$data.= "┠Nama :$name\n";
$data.= "┠Interface :$interface \n";
$data.= "┠lease-time :$lease \n";
$data.= "┠bootp-support :$bootp \n";
$data.= "┠authoritative :$authoritative \n";
$data.= "┠use-radius :$use_radius \n";
if ($dynamic == "true") {
$data .= "┠Dynamic : Iya \n";
} else {
$data .= "┠Dynamic : Tidak \n";
}
if ($disable == "true") {
$data .= "┗Status: ⚠ Disable\n";
} else {
$data .= "┗Status : ✔ Enable \n";
}
}
} else {
$texta = "Server or lease";
$keyboard = [['!Dhcp server', '!Dhcp lease'], ['Help',
'Sembunyikan'], ];
$replyMarkup = ['keyboard' => $keyboard, 'resize_keyboard' => true,
'setOneTimeKeyboard' => true, 'selective' => true];
$anu['reply_markup'] = json_encode($replyMarkup);
Bot::sendMessage($texta, $anu);
}
} else {
$data = "Tidak dapat Terhubung dengan Mikrotik Coba Kembali";
}
$replyMarkup = ['keyboard' => [], 'remove_keyboard' => true, 'selective' =>
false, ];
$anu['reply_markup'] = json_encode($replyMarkup);
return Bot::sendMessage($data, $anu);
});
//dns command
$bot->cmd('/dns|/Dns', function () {
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
}
return Bot::sendMessage($text);
});
// /traffic command
$bot->cmd('/traffic|traffic|/Traffic', function () {
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
));;
$tx = formatBites($getinterfacetraffic[0]['tx-bits-per-second'],1);
$rx = formatBites($getinterfacetraffic[0]['rx-bits-per-second'],1);
if ($maxtx == "" || $maxtx == "0") {
$mxtx = formatBites(100000000,0);
$maxtx = "100000000";
} else {
$mxtx = formatBites($maxtx,0);
$maxtx = $maxtx;
}
if ($maxrx == "" || $maxrx == "0") {
$mxrx = formatBites(100000000,0);
$maxrx = "100000000";
} else {
$mxrx = formatBites($maxrx,0);
$maxrx = $maxrx;
}
$Traffic .="Traffic\n";
$Traffic .="====================\n\n";
$Traffic .="Traffic ether1\n";
$Traffic .="TX: <code>$tx / $mxtx </code>\n";
$Traffic .="RX: <code>$rx / $mxrx </code>\n";
$Traffic .="====================\n\n";
//ulanggi lagi ether2
if ($API->connect($mikrotik_ip, $mikrotik_username,$mikrotik_password))
$getinterface = $API->comm("/interface/print");
$interface = $getinterface[0]['name'];
$getinterfacetraffic = $API->comm("/interface/monitor-traffic", array(
"interface" => "ether2", //traffic ether yang akan kita tampilkan
"once" => "",
));;
$tx = formatBites($getinterfacetraffic[0]['tx-bits-per-second'],1);
$rx = formatBites($getinterfacetraffic[0]['rx-bits-per-second'],1);
if ($maxtx == "" || $maxtx == "0") {
$mxtx = formatBites(100000000,0);
$maxtx = "100000000";
} else {
$mxtx = formatBites($maxtx,0);
$maxtx = $maxtx;
}
if ($maxrx == "" || $maxrx == "0") {
$mxrx = formatBites(100000000,0);
$maxrx = "100000000";
} else {
$mxrx = formatBites($maxrx,0);
$maxrx = $maxrx;
}
));;
$tx = formatBites($getinterfacetraffic[0]['tx-bits-per-second'],1);
$rx = formatBites($getinterfacetraffic[0]['rx-bits-per-second'],1);
if ($maxtx == "" || $maxtx == "0") {
$mxtx = formatBites(100000000,0);
$maxtx = "100000000";
} else {
$mxtx = formatBites($maxtx,0);
$maxtx = $maxtx;
}
if ($maxrx == "" || $maxrx == "0") {
$mxrx = formatBites(100000000,0);
$maxrx = "100000000";
} else {
$mxrx = formatBites($maxrx,0);
$maxrx = $maxrx;
}
));;
$tx = formatBites($getinterfacetraffic[0]['tx-bits-per-second'],1);
$rx = formatBites($getinterfacetraffic[0]['rx-bits-per-second'],1);
if ($maxtx == "" || $maxtx == "0") {
$mxtx = formatBites(100000000,0);
$maxtx = "100000000";
} else {
$mxtx = formatBites($maxtx,0);
$maxtx = $maxtx;
}
if ($maxrx == "" || $maxrx == "0") {
$mxrx = formatBites(100000000,0);
$maxrx = "100000000";
} else {
$mxrx = formatBites($maxrx,0);
$maxrx = $maxrx;
}
}else{
$Traffic="Tidak dapat terhubung ke mikrotik coba lagi";
}
$options = [
'parse_mode' => 'html',
'reply' => true,
];
return Bot::sendMessage($Traffic, $options);
});
//ping situs dari router test latency ke luar dan local
//anda bisa gunakan multi command contoh /ping ping PING Ping
//*Perbaikan Untuk bugs ping
$bot->cmd('/ping|ping|PING|Ping', function ($address){
Bot::sendMessage($datas);
return Bot::sendMessage($text);
} elseif (preg_match('/^[-a-z0-9]+\.[a-z]{2,6}$/', strtolower($address))) {
//*detect domain jalankan ini
$texta = "Mohon Ditunggu Permintaan Sedang Diproses";
Bot::sendMessage($texta);
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
///====================//*loopinglagi//====================///
$PING = $API->comm("/ping", array(
"address" => "10.150.1.8", //ip local target ping silahkan dirubah
"count" => "1",
));
$hot = $PING[0]['host'];
$status = $PING[0]['status'];
$size = $PING[0]['size'];
$ttl = $PING[0]['ttl'];
$time = $PING[0]['time'];
$packet_loss = $PING[0]['packet-loss'];
$avg = $PING[0]['avg-rtt'];
if ($status == 'timeout') {
$data = "PING WIFI 7 ⚠ Down \nHost :$hot Status : <b>$status</b>
Loss :$packet_loss%";
}
else {
$data = "PING WIFI 7 ✔ Reply \nHost :$hot Time : <b>$time</b> Loss :
$packet_loss%";
}
$options = ['parse_mode' => 'html', ];
Bot::sendMessage($data, $options); //hasil 7 akan dikirim
///====================//*SelesaIlooping//====================///
else {
$datas="Tidak dapat Terhubung dengan Mikrotik Coba Kembali";
}
$options = ['parse_mode' => 'html', ];
return Bot::sendMessage($datas, $options);
});
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
//ambil data
$text .= "\n♨ $interface\n";
$text .= "┠IP address : $address\n";
$text .= "┠Network : $network \n";
$text .= "┠interface : $interface \n";
//pecah kata true
if ($dynamic == "true") {
$text .= "┠Dynamic : Iya \n";
} else {
$text .= "┠Dynamic : Tidak \n";
}
}
}
return Bot::sendMessage($text);
});
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
$replyMarkup = [
];
$anu['reply_markup'] = json_encode($replyMarkup);
Bot::sendMessage($texta, $anu);
}
} else {
$text = "Tidak dapat Terhubung dengan Mikrotik Coba Kembali";
}
$replyMarkup = [
];
$anu['reply_markup'] = json_encode($replyMarkup);
return Bot::sendMessage($text, $anu);
});
// /resoure command
$bot->cmd('/resource|/Resource', function () {
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
$text.="<b>📡 Resource</b>\n";
$text.="<code>Boardname: $board</code>\n";
$text.="<code>Platform : $platform</code>\n";
$text.="<code>Uptime is: $uptime</code>\n";
$text.="<code>Cpu Load : $cpuload%</code>\n";
$text.="<code>Cpu type : $cpu</code>\n";
$text.="<code>Cpu Hz : $cpufreq Mhz/$cpucount core</code>\
n==========================\n";
$text.="<code>Free memory and memory \n$memory-$fremem/$mempersen %</code>\
n==========================\n";
$text.="<code>Free disk and disk \n$hdd-$frehdd/$hddpersen %</code>\
n==========================\n";
$text.="<code>Since reboot, bad blocks \n$sector-$setelahreboot/$kerusakan
%</code>\n==========================\n";
}
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
});
$bot->cmd('/Pool|/pool', function () {
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
});
//*Command Nambah user
/*
Cara Mengunakannya :
usagenya
Command spasi namaserver spasi userprofil spasi usernameuser spasi passworduser
+user server profil username password
contoh :
*/
$bot->cmd('+user', function ($server, $username, $password) {
$info = bot::message();
$id = $info['chat']['id'];
$iduser = $info['from']['id'];
$msgid = $info['message_id'];
$json = file_get_contents("data.json");
$json_a = json_decode($json, TRUE);
));
$texta = json_encode($add_user_api);
if (strpos(strtolower($texta), 'failure: already have user with this name
for this server') !== false) {
$gagal = $add_user_api['!trap'][0]['message'];
$text.= "⛔ Gagal Menginput user baru pastikan mengisikannya dengan
benar \n\n<b>KETERANGAN :</b>\n$gagal";
} elseif (strpos(strtolower($texta), 'ambiguous value of server, more than
one possible value matches input') !== false) {
$gagal = $add_user_api['!trap'][0]['message'];
$text.= "⛔ Gagal Menginput user baru pastikan mengisikannya dengan
benar \n\n<b>KETERANGAN :</b>\n$gagal";
} elseif (strpos(strtolower($texta), 'input does not match any value of
server') !== false) {
$gagal = $add_user_api['!trap'][0]['message'];
$text.= "⛔ Gagal Menginput user baru pastikan mengisikannya dengan
benar \n\n<b>KETERANGAN :</b>\n$gagal";
} else {
$text.= "Berhasil Diinput\n\n";
$text.= "<code>ID : $add_user_api</code>\n";
$text.= "<code>Server : $server</code>\n";
$text.= "<code>Name : $username</code>\n";
$text.= "<code>Password : $password</code>\n";
$dataid = str_replace('*', 'id', $add_user_api);
$text.= "RemoveNow : /rEm0v$dataid\n";
}
} else {
$text = "Tidak dapat Terhubung dengan Mikrotik Coba Kembali";
}
$options = ['parse_mode' => 'html', ];
return Bot::sendMessage($text, $options);
});
//$bot->cmd('!Generate', function () {
// Comingsiin
// return Bot::sendMessage($text, $options);
// $API->disconnect();
//});
$keyboard = [
['Monitor','Ping google.com'], ['Hotspot'],['Help','Sembunyikan'],
];
$replyMarkup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
'setOneTimeKeyboard' => true,
'selective' => true,
];
$anu['reply_markup'] = json_encode($replyMarkup);
return Bot::sendMessage($text, $anu);
});
$bot->cmd('Sembunyikan|!Sembunyikan', function ()
{
$text="disembunyikan";
$replyMarkup = [
'keyboard' => [],
'remove_keyboard' => true,
'selective' => false,
];
$anu['reply_markup'] = json_encode($replyMarkup);
return Bot::sendMessage($text, $anu);
});
$bot->on('new_chat_member', function() {
$info=bot::message();
$nama=$info['new_chat_member']['first_name'];
$grup=$info['chat']['title'];
$bot->run();