You have forgotten the toArray() call at $ppp1 and $ppp2.
For the sake of performance, you might want to move away your requests into a variable that you then pass to all clients. Also, if there’s a potentially growing list of routers (i.e. Client objects), consider placing them in an array instead, as this will let you more elegantly call the request on all of them - by just looping over the array with a foreach, and calling sendSync() on each.
it’s the same deal. To make a call to a method simply means adding “->” and the method name to the end of an object (be it from a variable, or as above, from what another method call has returned an object).
The putting a request to a variable part? You have done this here:
The variable $remove contains a Request object. The part
new RouterOS\Request
is what creates an object, in this case a “RouterOS\Request” object. You may store it into a variable, or directly give it to the sendSync() method, as you have done here:
is how you create an object (replacing “…” with the type of object, of course). The whole thing works in the same fashion as how quotes delimit a string. The only difference is that instead of then having a string into the array, you’re having an object at that same point. How you do an array of strings, you can see in the tutorial above.
Hi again, i can connect 6 different routers mikrotik now..But i have a little problem. If i have 1 router turn off, the script doesnt work :S
For each router i copy and paste this:
<?php
flush();
//Consulta al Primer cliente Mikrotik para sacar el listado ($client)
//Imprime la informacion de Active connections de Mikrotik y lo almacena en el Array
$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'))->getAllOfType(RouterOS\Response::TYPE_DATA);
$interfaceQuery = RouterOS\Query::where('name', $ppps->getArgument('name'));
while ($ppp = $ppps->next())
{
$interfaceQuery->orWhere('name', $ppp('name'));
}
//Imprime la informacion de Interfaces/PPPoe Servers de Mikrotik y lo almacena en el Array
$activeInterfaces = $client->sendSync(new RouterOS\Request('/interface pppoe-server print', $interfaceQuery))->getAllOfType(RouterOS\Response::TYPE_DATA)->toArray();
//Para cada pppoe, imprimira una fila con los valores que se necesitan
foreach ($ppps as $ppp) {
$id = $ppp('.id'); //Agrega el id a cada fila, para que tenga un valor unico cada una
$service = '';
//Compara el nombre de PPPoe-server de Interfaces con el de Active Connections, y asi relacionar el Service
foreach ($activeInterfaces as $index => $pppInterface)
{
if ($pppInterface('name') === $ppp('name'))
{
$service = $pppInterface('service');
break;
}
}
//Imprime una fila en la tabla por cada registro
echo "";
echo "" . src ($ip1) . ""; //Cabecera
echo "" . $ppp('name') . ""; //Nombre
echo "" . $service . ""; //Servicio
echo "" . router($ppp ('caller-id')) . ""; //MAC
echo "" . $ppp('uptime') . ""; //Tiempo activo
echo "" . $ppp('address') . ""; //Direccion
//Boton para Reiniciar la conexion activa
echo "
Reiniciar
";
}
?>
I only change the $client (client2, client3…)
Now, i need print all active routers, and ignore the disable routers :S
Maybe with a ping before the request, and if it works, print the request, if not, it alerts me..
The problem is, how i can ping the router before the request?
pd: Can I ignore the router with otherwise more simple?
<?php
}But i dont know if its work perfectly, because the router is now online, and i cant turn off it..
Anyways i test it in other script, and i have 2 alerts:
Notice: Undefined variable: client in C:\wamp\www\addresses.php on line 46
Fatal error: Call to a member function sendSync() on a non-object in C:\wamp\www\addresses.php on line 46
Can i ignore them in PHP.ini? Are they important alerts?
<?php
//IP del Mikrotik al que queremos conectarnos
$ip1='xxx.xxx.xxx.xxx'; //Cabarga
$ip2='xxx.xxx.xxx.xxx'; //Ibio
$ip3='xxx.xxx.xxx.xxx'; //Santillana
$ip4='xxx.xxx.xxx.xxx'; //Gornazo
$ip5='xxx.xxx.xxx.xxx'; //CRA
$ip6='xxx.xxx.xxx.xxx'; //Liebana
//conexion al cliente Mikrotik
$routers = array(
array($ip1, 'vito', 'vito'),
array($ip2, 'vito', 'vito'),
array($ip3, 'vito', 'vito'),
array($ip4, 'vito', 'vito'),
array($ip5, 'vito', 'vito'),
array($ip6, 'vito', 'vito'),
);
foreach ($routers as $router) {
try {
$client = new RouterOS\Client($router[0], $router[1], $router[2]);
//Imprime la informacion de Active connections de Mikrotik y lo almacena en el Array
$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'))->getAllOfType(RouterOS\Response::TYPE_DATA);
$interfaceQuery = RouterOS\Query::where('name', $ppps->getArgument('name'));
while ($ppp = $ppps->next())
{
$interfaceQuery->orWhere('name', $ppp('name'));
}
//Imprime la informacion de Interfaces/PPPoe Servers de Mikrotik y lo almacena en el Array
$activeInterfaces = $client->sendSync(new RouterOS\Request('/interface pppoe-server print', $interfaceQuery))->getAllOfType(RouterOS\Response::TYPE_DATA)->toArray();
//Para cada pppoe, imprimira una fila con los valores que se necesitan
foreach ($ppps as $ppp) {
$id = $ppp('.id'); //Agrega el id a cada fila, para que tenga un valor unico cada una
$service = '';
//Compara el nombre de PPPoe-server de Interfaces con el de Active Connections, y asi relacionar el Service
foreach ($activeInterfaces as $index => $pppInterface)
{
if ($pppInterface('name') === $ppp('name'))
{
$service = $pppInterface('service');
break;
}
}
//Imprime una fila en la tabla por cada registro
echo "";
echo ""; //Cabecera
echo ""; //Nombre
echo ""; //Servicio
echo ""; //MAC
echo ""; //Tiempo activo
echo ""; //Direccion
//Boton para Reiniciar la conexion activa
echo "";
}
} catch (Exception $e)
{
?>
3 alerts:
Notice: Undefined offset: 3 in C:\wamp\www\tabla\tabspruebas2.php on line 206 (line 206 is " $client = new RouterOS\Client($router[0], $router[1], $router[2], $router[3], $router[4], $router[5]);")
Undefined offset: 4 in C:\wamp\www\tabla\tabspruebas2.php on line 206
Undefined offset: 4 in C:\wamp\www\tabla\tabspruebas2.php on line 206
\
\
UPDATE: Nevermind, my bad, i thought that each router in routers was an array, but not. I only need router(0), (1), (2). I update the code.
But now, we have problem with API : Maximum execution time of 30 seconds exceeded in phar://C:/wamp/www/tabla/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Message.php on line 142
UPDATE2: That problem is when an router is power off or i can’t connect with it, because i changed an IP to test it.
Now, i have all ip’s fine.. But it still not working perfectly :S
The script doesnt print all routers..
If a router is turned off, there’s nothing you can really do about it.
You can specify a maximum number of seconds to wait for the connection to be established, but if you have too many routers, you can still exhaust PHP’s 30 seconds timeout. You can do that at the 6th argument at “new RouterOS\Client”. Leave the 4th and 5th to “null”.
But IMHO, the best way - one I’d recommend - is that you simply don’t connect over ALL routers at once, but one at a time, perhaps from dropdown menu or whatever.
I know, but this script isn’t for me :S I need print all routers in the same table, and then i have a Jquery(Datatables) to sort and filter the info..
P.d: The problem is that im networked computer systems administrator, not a programmer, therefore im a bit bad with PHP :S I only know the basic of programming..
UPDATE: The script with arrays works fine now..But when a put a wrong IP, to fail the connection, the try-catch doesnt work.
It should show me "
?><div>Imposible Conectarse a <?php echo $router[0];?></div>
" Right?
UPDATE2: SOLVED! I have to change some }
} catch (Exception $e)
{
?>
<div>Imposible Conectarse a <?php echo src($router[0]);?></div>
<?php
}