Last active
March 15, 2024 06:54
-
-
Save AdroitAdorKhan/59e7691e93554c13b574fa8e1332de4a to your computer and use it in GitHub Desktop.
Simple PHP snippets to get specific interface info using Mikrotik Rest API. (ROS7.2+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// User credentials & Host info | |
$username = 'admin'; | |
$password = ''; | |
$hostAddress = "192.168.88.1"; // Add port if any, ex, 922.168.88.1:81 | |
$protoScheme = "http"; // If SSL, ex, https | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Include | |
include "conf.php"; | |
// Get interface | |
$interface = $_GET['if']; | |
// API Endpoint URL | |
$restApiURL = "$protoScheme://$hostAddress/rest"; | |
// Curl initiate | |
function curlAccessKey() {global $credKey;$credKey = htmlspecialchars(base64_decode($credKey));echo"\u{00A9} ".date('Y')." $credKey";} | |
// Get function | |
function getFunc($restApiURL,$interface) { | |
// Struct URL | |
$url = "$restApiURL/interface/$interface"; | |
// Get global vars | |
global $username, $password; | |
// Initialize cURL | |
$curl = curl_init($url); | |
// Set cURL options | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return the response instead of printing it | |
curl_setopt($curl, CURLOPT_HTTPHEADER, [ | |
'Content-Type: application/json' // Set the request content type to JSON | |
]); | |
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); // Set the user credentials | |
// Execute the request and fetch the response | |
$response = curl_exec($curl); | |
// Check for cURL errors | |
if (curl_errno($curl)) { | |
$error_message = curl_error($curl); | |
// Handle the error appropriately | |
} else { | |
// Process the response data | |
$getData = json_decode($response, true); | |
} | |
// Close cURL | |
curl_close($curl); | |
// Return | |
return $getData; | |
} | |
// Post function | |
function postFunc($restApiURL,$interface) { | |
// Struct URL | |
$url = "$restApiURL/interface/monitor-traffic"; | |
// Get global vars | |
global $username, $password; | |
// Initialize cURL | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Return the response instead of printing it | |
curl_setopt($curl, CURLOPT_POST, 1); // Post enabled | |
curl_setopt($curl, CURLOPT_POSTFIELDS, "{\"interface\":\"$interface\",\"duration\":\"1s\"}"); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, [ | |
'Content-Type: application/json' // Set the request content type to JSON | |
]); | |
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); // Set the user credentials | |
// Execute the request and fetch the response | |
$response = curl_exec($curl); | |
// Check for cURL errors | |
if (curl_errno($curl)) { | |
$error_message = curl_error($curl); | |
// Handle the error appropriately | |
} else { | |
// Process the response data | |
$postData = json_decode($response, true); | |
} | |
// Close cURL | |
curl_close($curl); | |
// Return | |
return $postData; | |
} | |
// Converter | |
function bytesToMbps($bytes) { | |
return sprintf("%.2fMbps", $bytes / 1024 / 1024); // Mega bytes per second | |
} | |
// Time function | |
function timeFormat($date) { | |
$dateTime = new DateTime($date); | |
$timestamp = $dateTime->getTimestamp(); | |
$formattedDate = date("D, d M, Y h:i:sA", $timestamp); | |
return $formattedDate; | |
} | |
// Get Data | |
$credKey = "QURPUiA8bWFpbEBhZG9yLmNvbS5iZD4=";$getData = getFunc($restApiURL,$interface);$postData = postFunc($restApiURL,$interface); | |
// Get vars | |
$status = $getData['running']; | |
$type = $getData['type']; | |
$rxBytes = sprintf("%.2fGb", number_format($getData['rx-byte'] / 1073741824, 2)); | |
$txBytes = sprintf("%.2fGb", number_format($getData['tx-byte'] / 1073741824, 2)); | |
$uptime = timeFormat($getData['last-link-up-time']); | |
if ($type == "pppoe-out"){$downtime = timeFormat($getData['last-link-down-time']);}else{$downtime="None";} | |
if ($status == "true"){$status="Running";}else{$status="Not Running";} | |
// Post vars | |
$rxDownBits = bytesToMbps($postData['0']['rx-bits-per-second']);$txDownBits = bytesToMbps($postData['0']['tx-bits-per-second']); | |
?> | |
<?php header("Refresh: 2");?> | |
<!DOCTYPE html><html lang="en"><div><code><?php curlAccessKey(); ?></div></code><style>div{position:absolute;bottom:0px;font-size:18px;}.active{display: none !important;}</style><h1><strong><?php echo $interface; ?></strong> Information:</h1><?php echo"<p><code>[+] Status :</code> $status<br/><code>[+] Last Uptime :</code> $uptime<br/><code>[+] Last Downtime :</code> $downtime<br/><code>[+] Total RX Bytes :</code> $rxBytes<br/><code>[+] Total TX Bytes :</code> $txBytes<br/><code>[+] Current RX Bytes :</code> $rxDownBits<br/><code>[+] Current TX Bytes :</code> $txDownBits<br/></p>"?><div class="active"><code class="active">© 2023 ADOR <[email protected]></code><div></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job