The errors are due to your script trying to connect to the router despite the failed connection.
The best way to deal with this is to encapsulate your whole per-router script into a "try...catch" section, not just the "new RouterOS\Client" part.
For each router i copy and paste
Eww... Seriously dude! Arrays!!! They're your friends. Get to know them!
Add the details for each router into an array, and loop over it for each router. e.g.
<?php
$routers = array(
array('192.168.0.1', 'vito', 'vito'),
array('xxx.xxx.xxx.xxx', 'victor', 'victor'),
);
foreach ($routers as $router) {
try {
$client = new RouterOS\Client($router[0], $router[1], $router[2]);
//Rest of the per-router script
} catch (Exception $e)
{
?><div>Imposible Conectarse a <?php echo $router[0];?></div>
<?php
}
}
It looks nice! Thanks again
But i have a little problem again..
I have this now:
php code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Listado ByWifi</title>
<style type="text/css" title="currentStyle">
@import "tabs/css/demo_page.css";
@import "tabs/css/demo_table_jui.css";
@import "tabs/css/jquery-ui-1.8.4.custom.css";
@import "tabs/css/TableTools_JUI.css";
@import "tabs/css/selector.css";
.ui-tabs .ui-tabs-panel { padding: 10px }
</style>
</head>
<body>
<?php
//Conexion a la API de MIKROTIK
use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; //API Mikrotik PEAR2 mas info en : https://github.com/pear2/Net_RouterOS/wiki
//Botones de Modificar, Borrar y Añadir reglas de NAT
if (isset($_POST['act']))
{
foreach ($_POST['act'] as $act => $itemID)
{
if (in_array($act, array('set', 'remove', 'add')))
{
$actionRequest = new RouterOS\Request("/ppp/active/{$act}");
if ('add' !== $act) //Si es set o remove (distinto de add)
{
$actionRequest->setArgument('numbers', $itemID);
}
if ('set' === $act || 'add' === $act)
{
if (isset($_POST[$itemID]['chain']))
{
if ('srcnat' === $_POST[$itemID]['chain'])
{
$_POST[$itemID]['action'] = 'src-nat';
} elseif ('dstnat' === $_POST[$itemID]['chain'])
{
$_POST[$itemID]['action'] = 'dst-nat';
}
}
foreach ($_POST[$itemID] as $name => $value)
{
if ('' != $value)
{
$actionRequest->setArgument($name, $value);
}
}
}
$responses = $client->sendSync($actionRequest);
//Alertas de error
$errors = $responses->getAllOfType(RouterOS\Response::TYPE_ERROR);
if (0 === count($errors))
{
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
return;
} else
{
echo '<div><ul>';
foreach ($errors as $error)
{
echo '<li>' . $error('message') . '</li>';
}
echo '</ul></div>';
}
}
}
}
//FUNCION CABECERA
//Le da nombre a la cabecera que nos conectamos (Columna cabecera de la tabla)
function src($ip)
{
switch ($ip)
{
case 'x.x.x.x': //Primer Cliente
return "Cabarga";
break;
case 'x.x.x.x': //Segundo Cliente
return "Ibio";
break;
case 'x.x.x.x': //Tercer Cliente
return "Santillana";
break;
case 'x.x.x.x': //Cuarto Cliente
return "Gornazo";
break;
case 'x.x.x.x': //Quinto Cliente
return "CRA";
break;
case 'x.x.x.x': //Sexto Cliente
return "Liebana";
break;
}
}
//Funcion Router
function router($mac){
$x = substr($mac, 0, 5);
if ($x=='00:27' || $x=='DC:9F' || $x=='24:A4' )
{
return 'Ubiquiti';
}elseif ($x=='00:13'){
return 'Eminent';
}elseif ($x=='00:1D' || $x=='00:50'){
return 'DrayTek';
}elseif ($x=='00:0C' || $x=='00:21' || $x=='D4:CA' ){
return 'Mikrotik';
}elseif ($x=='B0:48' || $x=='54:E6' ){
return 'TPLink';
}elseif ($x=='C8:3A'){
return 'Tenda';
}elseif ($x=='00:15'){ //La mayoria son Ubiquity
return 'Ubiquiti';
}else{
return 'Desconocido';
}
}
?>
<!--Divs para el diseño y organizacion de la tabla-->
<body id="dt_example">
<div id="container">
<div id="demo">
<div id="tabs">
<div id="tabs-1">
<!--Formulario del listado NAT-->
<form action='' method='POST'>
<!--HTML de la Tabla, encabezado -->
<table cellpadding="0" cellspacing="0" border="0" class="display" id="tabla1">
<thead><tr>
<th>Cabecera</th>
<th>Nombre</th>
<th>Servicio</th>
<th>Modelo</th>
<th>Tiempo Activo</th>
<th>Direccion</th>
<th>Reiniciar</th>
</tr></thead><tbody>
<?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 "<tr>";
echo "<td>" . src ($router[0]) . "</td>"; //Cabecera
echo "<td>" . $ppp('name') . "</td>"; //Nombre
echo "<td>" . $service . "</td>"; //Servicio
echo "<td>" . router($ppp ('caller-id')) . "</td>"; //MAC
echo "<td>" . $ppp('uptime') . "</td>"; //Tiempo activo
echo "<td>" . $ppp('address') . "</td>"; //Direccion
//Boton para Reiniciar la conexion activa
echo "<td>
<button type='submit' value='{$id}' name='act[remove]'>Reiniciar</button>
</td>
</tr>";
}
} catch (Exception $e)
{
?><div>Imposible Conectarse a <?php echo $router[0];?></div>
<?php
}
}
?>
</tbody></table></form>
</div>
</div>
</div>
</div>
<!--jQuerys DATATABLE // Llamada a todos los javascripts necesarios-->
<script type="text/javascript" charset="utf-8" src="tabs/js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="tabs/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="tabs/js/jquery-ui-tabs.js"></script>
<script type="text/javascript" charset="utf-8" src="tabs/js/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="tabs/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8">
...
</script>
</body>
</html>
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..