Community discussions

MikroTik App
 
User avatar
stefki
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Mon Aug 29, 2016 2:13 pm

RouterOS v7.6 php api changed

Fri Nov 25, 2022 11:41 am

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

thank you
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: RouterOS v7.6 php api changed

Fri Nov 25, 2022 5:40 pm

Here is one way to do it. (I guess rextended will comment the on-error :D )
On RouterOS v7 temperatur and volt are stored differently, so this script get it from both version.
Look at my complete script for Splunk here:
viewtopic.php?t=179960
: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"
	}
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: RouterOS v7.6 php api changed

Sat Nov 26, 2022 8:43 am

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"
	}
}
}
 
User avatar
stefki
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Mon Aug 29, 2016 2:13 pm

Re: RouterOS v7.6 php api changed

Sat Nov 26, 2022 1:28 pm

@jotne thank you but I need php code.
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: RouterOS v7.6 php api changed

Mon May 22, 2023 4:28 pm

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?
 
User avatar
ID
just joined
Posts: 21
Joined: Tue Dec 26, 2006 10:36 pm

Re: RouterOS v7.6 php api changed

Mon May 22, 2023 9:44 pm

Probably you are using routeros_api.class.php and updated while ago.
Take a look at https://github.com/BenMenking/routeros- ... .class.php
It's backward routeros compatible.
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: RouterOS v7.6 php api changed

Mon May 22, 2023 11:05 pm

@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
 
User avatar
ID
just joined
Posts: 21
Joined: Tue Dec 26, 2006 10:36 pm

Re: RouterOS v7.6 php api changed

Tue May 23, 2023 11:03 am

My bad. I didn't think that you are using pear.
Follow https://github.com/pear2/Net_RouterOS
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: RouterOS v7.6 php api changed

Tue May 23, 2023 9:26 pm

@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?
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: RouterOS v7.6 php api changed

Thu May 25, 2023 7:55 pm

Probably you are using routeros_api.class.php and updated while ago.
Take a look at https://github.com/BenMenking/routeros- ... .class.php
It's backward routeros compatible.
@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
<?php

require('../routeros_api.class.php');

$API = new routerosAPI();

$API->debug = true;
$username = 'diye';
if ($API->connect('172.16.7.3', 'admin', 'password@1')) {

$API->write('/tool/user-manager/user/print', false);
$API->write('=.proplist=.id', false);
//print_r($edituser);
$API->write('?username='.$username .' ');
$API->write('/tool/user-manager/user/set', false);
$API ->write('=.id=*13', false);
$API->write('=password=1506');
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
print_r($ARRAY);
$API->disconnect();
}
?>

Connection attempt #1 to 1.1.1.1:8728... <<< [6] /login <<< [11] =name=admin <<< [20] =password=password >>> [5/5] bytes read. >>> [5, 1]!done Connected... <<< [29] /tool/user-manager/user/print <<< [14] =.proplist=.id <<< [14] ?username=diye <<< [27] /tool/user-manager/user/set <<< [8] =.id=*13 <<< [14] =password=1506 >>> [3/3] bytes read. >>> [3, 24]!re >>> [8/8] bytes read. >>> [8, 15]=.id=*13 >>> [5/5] bytes read. >>> [5, 8]!done >>> [5/5] bytes read. >>> [5, 1]!done Array ( [0] => Array ( [.id] => *13 ) ) Disconnected... Disconnected...
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: RouterOS v7.6 php api changed

Sun Jun 25, 2023 7:45 pm

I have used python paramiko lib to update users to mikrrotik usermanager.It's easier for me

Who is online

Users browsing this forum: No registered users and 20 guests