Community discussions

 
sw0rdf1sh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Sun Nov 28, 2010 6:16 pm

Mikrotik PHP-API

Tue Mar 22, 2011 2:34 am

As I was checking out the API I made a small interface for monitoring one router.

I added 5 menus:
1.Resources
2.Basic list of interfaces
3.Wireless Interfaces
4.Registration Table
5.Hotspot/Hosts


All you need to do is change the ip/user/pwd of your RB in the 5 php files (and ofcourse open port for the api in /ip/services/ in your RB).-Sorry you have to do that in 5 sections and not in one php file.... :)

The values (like in.ex. signal strength) are not stored in a database as an array.They are just displayed dynamically as a user requests the php files.
This uses the php api class in the wiki:
http://wiki.mikrotik.com/wiki/API_PHP_class
I also used overLIB popup JavaScript library (it's included in the rar) ,merging some values together, because the tables where way to long for my screen sometimes.

Hope you like it.This is as simple as it gets (basic hmtml,php,javascript) but shows how easy it is to get the values and make an interface.
mikrotik-php-api.rar
You do not have the required permissions to view the files attached to this post.
Last edited by sw0rdf1sh on Wed Mar 23, 2011 1:28 pm, edited 1 time in total.
 
sw0rdf1sh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Sun Nov 28, 2010 6:16 pm

Re: Mikrotik PHP-API

Tue Mar 22, 2011 10:31 pm

I have a few comments I would like to post for anyone with the php knowledge who wants to deal with it:
Let's sat we want to use php-api as a display sollution.

The above api-php-class works even in a shared hosting environment (tested it and works very well)
This is because of the brilliant use of fsockopen in the php-class -regards to the user who wrote that! 8) which lets a shared hosting environment php script call mikrotik in port 8728.
Even though it's not really secure in a shared hosting to call other port than 80 it's easy to use and you can provide information (let's say about a hotspot, or signals - I don't suggest providing mac addresses of your users :shock: even though I added every value you can just erase in the table the columns of sensitive values) in a http://www.domain.com . It also works on dyndns hosts as long as your modem redirects the port 8728 to your routerboard.

Things simplified are:
If you are in a shared hosting:
1.Upload , let's say, total users connected php in http://www.domain.com
2.Users visit your domain viewing php script
3.php script calls mikrotik api
4.php returning array of values for what u asked (command /interface/registration-table/print) and php displays in your domain a value from that array (which in this case is the number of total users connected to your mikrotik).

Now if you want to monitor the above number through time (let's say that you want to show a graph of total users in you domain.com) you would need an array of values for total users through time.
That's why you would propably need a cron job running the php script every 15min or so, a database to store those values every 15min, and a php graph script like the "PHPGraphLib" found here http://www.ebrueggeman.com/phpgraphlib/examples.php to display/graph those values from your database.

I know it's a lot and it's more like a php question but if done provides a way of graphing externally (even in shared hosting) any value that can be retrieved with the mikrotik-api.Just a thought but if anyone having the php knowledge and the time would like to try it would be great.

PS:I need coffee... :shock:
 
chapeupreto
just joined
Posts: 6
Joined: Wed Jul 21, 2010 5:49 pm

Re: Mikrotik PHP-API

Tue Mar 22, 2011 11:05 pm

Hi folks. What's up?!
That is really an amazing and useful PHP class that I've been using a lot in my projects.

Well.. I only got this simple question about it.
After executing a script that uses the routeros_api.class.php class, I got this message:
Notice: Undefined variable: _ in ... on line 313
Then, I would like to ask you guys a question:
First, consider the following piece of code which exists in that php class (lines 300 up to 320):
         // If we have got more characters to read, read them in.
         if ($LENGTH > 0) {
            $_ = "";
            $retlen=0;
            while ($retlen < $LENGTH) {
               $toread = $LENGTH - $retlen ;
               $_ .= fread($this->socket, $toread);
               $retlen = strlen($_);
            }
            $RESPONSE[] = $_ ;
            $this->debug('>>> [' . $retlen . '/' . $LENGTH . ' bytes read.');
         }

         // If we get a !done, make a note of it.
         if ($_ == "!done")
            $receiveddone=true;

         $STATUS = socket_get_status($this->socket);

         
         if ($LENGTH > 0)
            $this->debug('>>> [' . $LENGTH . ', ' . $STATUS['unread_bytes'] . '] ' . $_);
So, there is this PHP variable $_ on it.
Besides the code above, I've glanced the entire class and saw no other mentions for that $_
I suppose my script don't execute those instructions inside that first conditional, i.e., if ($LENGTH > 0) ...
Therefore, when the script reachs that second conditional, i.e., if ($_ == "!done"), the PHP produces the notice message
undefined variable: _ 
, just because the $_ variable had never been initialized.
Although it's just a simple notice message, is that code right? I mean, don't you think the $_ variable should be initialized somewhere before the execution of those IF's?
That's it.
Thanks for any reply.

rod~
 
sw0rdf1sh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Sun Nov 28, 2010 6:16 pm

Re: Mikrotik PHP-API

Wed Mar 23, 2011 1:11 am

Hi folks. What's up?!
That is really an amazing and useful PHP class that I've been using a lot in my projects.
Can you share any of them?
After executing a script that uses the routeros_api.class.php class, I got this message:
Notice: Undefined variable: _ in ... on line 313
I think you should post for a solution where the api class was originally posted in scripts. It wasn't written by me but just so you know I've never had this kind of message.I belive the variable is set (at first time empty) in:
$_ = "";
it might be a register_globals issue. Turn it on and see if you still have the same errors.Or if on shared hosting prob. you can't change much.
 
chapeupreto
just joined
Posts: 6
Joined: Wed Jul 21, 2010 5:49 pm

Re: Mikrotik PHP-API

Wed Mar 23, 2011 4:28 pm

Hi sw0rdf1sh

Thanks for replying me.
Can you share any of them?
Sure! I could mention some simple stuffs, like adding MAC Addresses in 'Wireless Tables -> Access List'.
Also, adding and removing simple queues for my users.
And mainly, adding and removing 'Address List' in the Mikrotik's Firewall section.
I think you should post for a solution where the api class was originally posted in scripts. It wasn't written by me but just so you know I've never had this kind of message.
I see. As a matter of fact, I'm posting this same question on this thread here:
http://forum.mikrotik.com/viewtopic.php?f=9&t=25445
Maybe I could get some answer.
I belive the variable is set (at first time empty) in:
$_ = "";
I understand. However, by reading that code, if my first 'if ()' evaluates to a false value, then my $_ won't be set. This might cause that notice message as mentioned earlier.
It might be a register_globals issue.
Unfortunately, I can't change that option.

Thanks bro!

rod~
 
gkwyc
just joined
Posts: 2
Joined: Sun Nov 04, 2012 1:42 am

Re: Mikrotik PHP-API

Sun Nov 04, 2012 5:50 am

can not use

Hi sw0rdf1sh

Thanks for replying me.
Can you share any of them?
Sure! I could mention some simple stuffs, like adding MAC Addresses in 'Wireless Tables -> Access List'.
Also, adding and removing simple queues for my users.
And mainly, adding and removing 'Address List' in the Mikrotik's Firewall section.
I think you should post for a solution where the api class was originally posted in scripts. It wasn't written by me but just so you know I've never had this kind of message.
I see. As a matter of fact, I'm posting this same question on this thread here:
http://forum.mikrotik.com/viewtopic.php?f=9&t=25445
Maybe I could get some answer.
I belive the variable is set (at first time empty) in:
$_ = "";
I understand. However, by reading that code, if my first 'if ()' evaluates to a false value, then my $_ won't be set. This might cause that notice message as mentioned earlier.
It might be a register_globals issue.
Unfortunately, I can't change that option.

Thanks bro!

rod~
 
wencs
just joined
Posts: 10
Joined: Wed Mar 26, 2014 7:14 pm

Re: Mikrotik PHP-API

Sun Oct 25, 2015 10:05 am

cada quien tiene su manera de programar yo e tomado el codigo y e corregido de esta forma trabaja muy bien

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hotspot/Hosts in Mikrotik</title>
<style type="text/css">
<!--
a:link {text-decoration: none;}
a:visited {text-decoration: none;}
a:hover {text-decoration: none;}
a:active {text-decoration: none;}
-->
</style></head>
<body link="#006600">
<?php
function popup( $text, $popup ){
?>
<a href="javascript:void(0);" onmouseover="return overlib('<?php echo($popup); ?>');" onmouseout="return nd();"><?php echo($text); ?></a>
<?php
}
?>
<script type="text/javascript" src="overlib/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
<?php
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = false;
$API->port = 2086;
$servidor="172.16.0.1";
$usuario="admin";
$clave="88708870";
if ($API->connect($servidor, $usuario, $clave)) {
$ARRAY = $API->comm("/ip/hotspot/host/print");
$registros=count($ARRAY);
echo "<table width=100% border=1>";
echo "<tr><td align=left size=3>Id</td><td size=3>mac-address</td><td size=3>address</td><td size=3>to-address</td><td>server</td><td>uptime</td><td>keepalive-timeout</td><td>found-by</td><td>DHCP</td><td>authorized</td><td>bypassed</td><td>comment</td></tr>";
echo "<tr><td align=left>";
for ($i=0; $i<$registros; $i++){
$regtable = $ARRAY[$i];
//
if (!isset($regtable['.id'])) { $regtable['.id']="";}
if (!isset($regtable['mac-address'])) { $regtable['mac-address']="";}
if (!isset($regtable['address'])) { $regtable['address']="";}
if (!isset($regtable['to-address'])) { $regtable['to-address']="";}
if (!isset($regtable['server'])) { $regtable['server']="";}
if (!isset($regtable['uptime'])) { $regtable['uptime']="";}
if (!isset($regtable['keepalive-timeout'])) { $regtable['keepalive-timeout']="";}
if (!isset($regtable['found-by'])) { $regtable['found-by']="";}
if (!isset($regtable['DHCP'])) { $regtable['DHCP']="";}
if (!isset($regtable['authorized'])) { $regtable['authorized']="";}
if (!isset($regtable['bypassed'])) { $regtable['bypassed']="";}
if (!isset($regtable['comment'])) { $regtable['comment']="";}
//
echo "<tr>";
echo "<td align=left><font color=#04B404 size=3>" . $regtable['.id'] . "</font></td>";
echo "<td align=left><font color=#04B404 size=3>" . $regtable['mac-address'] . "</font></td>";
echo "<td align=left><font color=#04B404 size=3>" . $regtable['address'] . "</font></td>";
echo "<td align=left><font color=#04B404 size=3>" . $regtable['to-address'] . "</font></td>";
echo "<td align=left><font color=#000099 size=3>" . $regtable['server'] . "</font></td>";
echo "<td align=left><font color=#003300 size=3>" . $regtable['uptime'] . "</font></td>";
echo "<td align=left><font color=#003300 size=3>" . $regtable['keepalive-timeout'] . "</font></td>";
echo "<td align=left><font color=#880000 size=3>" . $regtable['found-by'] . "</font></td>";
if ($regtable['DHCP']=="true") {
echo "<td align=left><font color=#04B404 size=3>" . $regtable['DHCP'] . "</font></td>";
}else{
echo "<td align=left><font color=#FF0000 size=3>" . $regtable['DHCP'] . "</font></td>";

}
if ($regtable['authorized']=="true") {
echo "<td align=left><font color=#04B404 size=3>" . $regtable['authorized'] . "</font></td>";
}else{
echo "<td align=left><font color=#FF0000 size=3>" . $regtable['authorized'] . "</font></td>";
}
if ($regtable['bypassed']=="true") {
echo "<td align=left><font color=#04B404 size=3>" . $regtable['bypassed'] . "</font></td>";
}else{
echo "<td align=left><font color=#FF0000 size=3>" . $regtable['bypassed'] . "</font></td>";
}
echo "<td align=left><font color=#04B404 size=3>" . $regtable['comment'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
$API->disconnect();
}
?>
</body>
</html>

Who is online

Users browsing this forum: No registered users and 9 guests