Hello, I need some advice of php developer. Recently I have upgrade my RB4011 to v7.6
Now I am unable to retrieve the voltage and temperature of my router in php.
This code properly worked on my RouterOS v6 .
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
require_once 'email.php';
try {
$client = new RouterOS\Client('10.2.3.3', 'admin', 'admin771');
} catch (Exception $e) {
die('Unable to connect to the router.');
//Inspect $e if you want to know details about the failure.
}
$responses = $client->sendSync(new RouterOS\Request('/system/health/print'));
foreach ($responses as $response) {
if ($response->getType() === RouterOS\Response::TYPE_DATA) {
$voltage= $response->getProperty('voltage');
$temp= $response->getProperty('temperature');
}
}
echo 'Voltage: ', $voltage, 'V';
echo "<br>";
echo 'Temperature: ', $temp, 'C';
The output of RouterOS v6.49
/system health print
voltage: 11.9V
temperature: 28C
Now in RouterOS v7.6
the output is bit different
/system/health/print
Columns: NAME, VALUE, TYPE
# NAME VALUE TYPE
0 voltage 27.2 V
1 temperature 44 C
If someone could help with php code how I can “echo” the voltage and temperature on RouterOS v7.6
:do {
# New version
:foreach id in=[/system health find] do={
:local health "$[/system health get $id]"
:set ( "$health"->"script" ) "health"
:log info message="$health"
}
} on-error={
# Old version
:if (!([/system health get]~"(state=disabled|^\$)")) do={
:local health "$[/system health get]"
:set ( "$health"->"script" ) "health"
:log info message="$health"
}
}
Updated version that checks for main RouterOS version, to select the correct part to run:
{
:local train [:tonum [:pick [/system resource get version] 0 1]]
:if ($train > 6) do={
# New version (RouterOS >6)
:foreach id in=[/system health find] do={
:local health "$[/system health get $id]"
:set ( "$health"->"script2" ) "health"
:log info message="$health"
}
} else={
# Old version (RouterOS 6 or older)
:if (!([/system health get]~"(state=disabled|^\$)")) do={
:local health "$[/system health get]"
:set ( "$health"->"script2" ) "health"
:log info message="$health"
}
}
}
Hello Everyone,
I have two mikrotik routers version 6.42 and 6.49.My php api codes works for 6.42 but for 6.49,it shows an error “invalid username or password”. I searched. on google and found out there’s an updated php api to access Mikrotik Router OS for higher versions
pls, how did you connect to version 7.6?
@id
thanks a lot for the update. I am a bit confused. I am using pear2/net/router os. So i should discard that and work with routeros_api.class.php. Do i need to install composer? because i am seeing composer.json file among the files in the link you sent to me
i await your kind response
@id
thanks for the swift response.I have checked the link. I have downloaded the files.I will try them tomorrow.
Meanwhile,i checked one of files and found this
use PEAR2\Net\RouterOS;
require_once ‘PEAR2/Autoload.php’;
try {
$client = new RouterOS\Client(‘192.168.88.1’, ‘admin’, ‘password’);
} catch (Exception $e) {
die(‘Unable to connect to the router.’);
}
The above codes are the same with the old version.Will it work with the newer versions?
@ID
sorry to bother you again. The routeros_class_api.php is connecting to mikrotik router unlike pear2/net/routeros. I want to set the password of an existing user in the usermanager. I have been able to get .id but i cant store it in a variable
see code below