Community discussions

MikroTik App
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

print ppp active connections and interface list PEAR2 PHP

Wed Oct 23, 2013 1:50 pm

Hi guys, im here again, with a new code:

php code

<?php

use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

							//IP MIKROTIK	//Usuario	//Password
$client = new RouterOS\Client('xxx.xxx.xxx.xxx, 'admin', 'admin'); //Conexion a Mikrotik 

// Tabla
echo "<table align='center' border='1' bordercolor='black'><form action='' method='POST'>";
echo "<tr bgcolor='#D8D8D8'><td align=left size=3>Nombre</td><td align=left size=3>Servicio</td><td size=3>Tiempo Activo</td>
<td align=left size=3>Direccion</td><td align=left size=3>Reiniciar</td></tr>";

//Actualizar pagina
echo "<meta http-equiv='refresh' content='2'>";


$interfaces = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print'));
$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'));


if ($interfaces['name']==$ppps['name']) {
	foreach (array_combine($interfaces, $ppps) as $interface => $ppp) {
		if ($interface->getType() === RouterOS\Response::TYPE_DATA OR $ppp->getType() === RouterOS\Response::TYPE_DATA ) {
		$id = $interface('.id');
		$id2 = $ppp ('.id');
		echo "<tr>";
		echo "<td>". $interface('name') ."</td>";
		echo "<td>" . $interface('service'). "</td>";
		echo "<td>" . $interface('uptime'). "</td>";
		echo "<td>". $ppp('address') ."</td>";
		echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
	}
	}
}

echo  "</form></table>"

?>
This time, i need a list with ppp active connections, service, uptime, address, and a reboot connection button.
I think, i need combine 2 print arrays(pppoe-server and active ppp), but dont print nothing :S
And i don know how to reboot the active connection with API.

Thanks :)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 2:16 pm

I'm not using PPP, so I don't know for sure, but... doesn't the "active" entry contain all the information you need? Why lookup the non-active interfaces' info to begin with?

As for the combine... it doesn't work, because the response from sendSync() is not an array, but a ResponseCollection object.

Speaking of which, you can more easily print the data responses by using its getAllOfType() method, e.g.

php code

<?php

use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

							//IP MIKROTIK	//Usuario	//Password
$client = new RouterOS\Client('xxx.xxx.xxx.xxx, 'admin', 'admin'); //Conexion a Mikrotik 

// Tabla
echo "<table align='center' border='1' bordercolor='black'><form action='' method='POST'>";
echo "<tr bgcolor='#D8D8D8'><td align=left size=3>Nombre</td><td align=left size=3>Servicio</td><td size=3>Tiempo Activo</td>
<td align=left size=3>Direccion</td><td align=left size=3>Reiniciar</td></tr>";

//Actualizar pagina
echo "<meta http-equiv='refresh' content='2'>";

$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'))->getAllOfType(RouterOS\Response::TYPE_DATA);

foreach ($ppps as $ppp) {
	$id = $ppp('.id');
	echo "<tr>";
	echo "<td>". $ppp('name') ."</td>";
	echo "<td>" . $ppp('service'). "</td>";
	echo "<td>" . $ppp('uptime'). "</td>";
	echo "<td>". $ppp('address') ."</td>";
	echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
}

echo  "</form></table>"

?>
If you're sure you want to go on your current route... the ResponseCollection also has a toArray() method, so
array_combine($interfaces->toArray(), $ppps->toArray()) 
could work... except that array_combine wants both arrays to have equal number of elements, meaning that unless all of your PPP interfaces are active, the above would return "false" (which would then trigger a fatal error if given to "foreach").
And i don know how to reboot the active connection with API.
How do you do it from CLI and/or Winbox?
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 3:52 pm

Hmmm.. I need 'service' from /interface/pppoe-server instead 'service' from /ppp/active and name (interface/ppoe-server)= name(ppp/active), and i use 'if' like this:
$interfaces = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print')); 
$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'))->getAllOfType(RouterOS\Response::TYPE_DATA);

if ($interfaces['name'] == $ppps['name']){
foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  echo "<tr>";
  echo "<td>". $ppp('name') ."</td>";
 echo "<td>" . $ppp('service'). "</td>";
 echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>";
  echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
}
}
And now, how i change the service? I dont understand.. It should be $interface('name') but i dont know how i put the array interfaces in the same foreach :S

How do you do it from CLI and/or Winbox?
In ppp/active connections, i remove the connection, and it restarts. So i need something like
$client->sendSync(new RouterOS\Request('/ppp/active/remove')) in the button
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 4:35 pm

The two "service" fields are different?!?

OK then... one way to do it is to first get a list of all active interfaces, then for each one, make a print against the full list with a query where you ask for the name to be the same. For example:
$ppps = $client->sendSync(new RouterOS\Request('/ppp/active/print'))->getAllOfType(RouterOS\Response::TYPE_DATA);

foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  $service = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print', RouterOS\Query::where('name', $ppp('name')))->getArgument('service');
  echo "<tr>";
  echo "<td>". $ppp('name') ."</td>";
  echo "<td>" . $service . "</td>";
  echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>";
  echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
} 
P.S. I've just realized a missing feature with my client - you could currently just get the two lists of active and all interfaces, but the two lists are not sorted or indexed in any way, so there's not currently a quick way to use one as reference for searching through the other. There's a possible remedy for this (in fact, I can think of several, and the real question is which one serves most use cases while being the most efficient), that I'd be sure to implement in the next version. So while the above would still work, you'd get better performance with that other method.

P.S.S. The number 1 reason I help on this forum is exactly to find out where the limits of my client are... and then break them! That question here did that, so I guess I should say "Thank you" :D .
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 5:10 pm

The code print a bit of data (5 or 6 rows), but then i have this error:

"Fatal error: Maximum execution time of 30 seconds exceeded in phar://C:/wamp/www/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Communicator.php on line 601"
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 5:26 pm

Yeah... like I said, the above is not exactly a very efficient way... I'll implement a more efficient one this weekend or so, and perhaps you could try it out then.

In the meantime, the only workaround is that you simply increase your execution timeout to more than 30 seconds, but that's obviously not a good idea in the long run.
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 5:41 pm

Hmmm but, how i can increase the execution timeout? im using PEAR2_Net_RouterOS-1.0.0b4.phar
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 5:46 pm

Using PHP's set_time_limit() function.

Ideally, use it within the loop itself, to ensure the script would always finish, e.g.
foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  $service = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print', RouterOS\Query::where('name', $ppp('name')))->getArgument('service');
  echo "<tr>";
  echo "<td>". $ppp('name') ."</td>";
  echo "<td>" . $service . "</td>";
  echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>";
  echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
  set_time_limit(5);//Give the script additional 5 seconds for the next request
}  
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 6:10 pm

After 5-10 minutes, the script finished but we have another error xD

"Fatal error: Maximum execution time of 5 seconds exceeded in phar://C:/wamp/www/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/Transmitter/NetworkStream.php on line 120"
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 6:20 pm

If i dont compare the 'name's, it should be more fast, right? How is the code?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Wed Oct 23, 2013 7:15 pm

If you don't compare the names, you have absolutely no guarantee as to which interface you're targeting - you could end up with the service of one interface being shown as if it is the service of a completely unrelated one.

You could get your full list at once, and then not make any further ones. In that case, you'd then have to loop over the full list for every active interface, which isn't much more efficient than doing a mini print request. In fact, as far as memory consumption goes, it's much worse, i.e. you're trading a few seconds saved for lots of MBs of RAM, perhaps hundreds, depending on how large your full list is. To minimize the memory footprint, you could construct a query in advance.

So perhaps:
$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'));
}

$activeInterfaces = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print', $interfaceQuery))->getAllOfType(RouterOS\Response::TYPE_DATA)->toArray();

foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  $service = '';
  foreach ($activeInterfaces as $index => $pppInterface) {
    if ($pppInterface('name') === $ppp('name')) {
      $service = $pppInterface('service');
      unset($activeInterfaces[$index]);
      break;
    }
  }
  echo "<tr>";
  echo "<td>". $ppp('name') ."</td>";
  echo "<td>" . $service . "</td>";
  echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>";
  echo "<td><input type='submit' value='Reiniciar' name='Reiniciar' /></td></tr>";
}  
EDIT: Added an unset and break within the check loop - should improve performance a little.
Last edited by boen_robot on Thu Oct 24, 2013 3:50 pm, edited 1 time in total.
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 3:48 pm

Nice, its working, it takes about 20 seconds, but its ok.

Now i need remove the active connections with the restart button.

Something like /ppp/active/remove..
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 3:52 pm

Nice, its working, it takes about 20 seconds, but its ok.

Now i need remove the active connections with the restart button.

Something like /ppp/active/remove..
That's exactly the command - make such a request where you set the "numbers" argument to the ID of the active connection (the one in the variable $id), after you process the $_POST, just like in your firewall form (please don't make me write out all the code for you... I'm trying to help you learn).

(BTW: Note the edit above, for performance boost)
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 4:10 pm

Nice, its working, it takes about 20 seconds, but its ok.

Now i need remove the active connections with the restart button.

Something like /ppp/active/remove..
That's exactly the command - make such a request where you set the "numbers" argument to the ID of the active connection (the one in the variable $id), after you process the $_POST, just like in your firewall form (please don't make me write out all the code for you... I'm trying to help you learn).

(BTW: Note the edit above, for performance boost)
ok ok i will try :)

P.D: I run the script again and.." Fatal error: Maximum execution time of 30 seconds exceeded in phar://C:/wamp/www/PEAR2_Net_RouterOS-1 ... ection.php on line 218"
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 4:13 pm

P.D: I run the script again and.." Fatal error: Maximum execution time of 30 seconds exceeded
Fuuuuu....

OK... remove the "unset" line. Apparently, freeing memory takes more time than a loop over your full list.

(Note to self...)
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 4:43 pm

P.D: I run the script again and.." Fatal error: Maximum execution time of 30 seconds exceeded
Fuuuuu....

OK... remove the "unset" line. Apparently, freeing memory takes more time than a loop over your full list.

(Note to self...)
I haven't the unset line.. I just paste your last code

P.D: Yes i have sorry :S Much better now, 10-12 seconds

P.D.2: I post my last code

php code

if (isset($_POST['act'])) {
    $acts = $_POST['act'];

	foreach ($acts as $act => $itemID) {
	
	$remove=new RouterOs\Request("/ppp/active/remove");
	
	}




// Tabla
echo "<table align='center' border='1' bordercolor='black'><form action='' method='POST'>";
echo "<tr bgcolor='#D8D8D8'><td align=left size=3>Nombre</td><td align=left size=3>Servicio</td><td size=3>Tiempo Activo</td><td align=left size=3>Direccion</td><td align=left size=3>Reiniciar</td></tr>";

//Actualizar pagina
//echo "<meta http-equiv='refresh' content='30'>";


$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'));
}

$activeInterfaces = $client->sendSync(new RouterOS\Request('/interface/pppoe-server/print', $interfaceQuery))->getAllOfType(RouterOS\Response::TYPE_DATA)->toArray();

foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  $service = '';
  foreach ($activeInterfaces as $index => $pppInterface) {
    if ($pppInterface('name') === $ppp('name')) {
      $service = $pppInterface('service');
      break;
    }
 }
  echo "<tr>";
  echo "<td><input type='text' name='{$id}[name]' value='". $ppp('name') ."' /></td>";
  echo "<td><input type='text' name='{$id}[service]' value='" . $service . "' /></td>";
  echo "<td><input type='text' name='{$id}[uptime]' value='" . $ppp('uptime'). "' /></td>";
  echo "<td><input type='text' name='{$id}[address]' value='". $ppp('address') ."' /></td>";
  echo "<td><button type='submit' value='{$id}' name='remove' >Reiniciar</td></tr>";
} 

echo  "</form></table>";

echo  "</form></table>";

?>
Im not sure how to put the button working sorry :S
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Thu Oct 24, 2013 6:30 pm

Like I said, just add the argument "numbers" with the item's ID as value, i.e.
$remove=new RouterOs\Request("/ppp/active/remove");
$remove->setArgument('numbers', $itemID); 
and then just send $remove.


BTW, since you can't alter an active connection, you may want to restore
  echo "<td><input type='text' name='{$id}[name]' value='". $ppp('name') ."' /></td>";
  echo "<td><input type='text' name='{$id}[service]' value='" . $service . "' /></td>";
  echo "<td><input type='text' name='{$id}[uptime]' value='" . $ppp('uptime'). "' /></td>";
  echo "<td><input type='text' name='{$id}[address]' value='". $ppp('address') ."' /></td>"; 
to your initial
  echo "<td>". $ppp('name') ."</td>";
  echo "<td>" . $service . "</td>";
  echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>"; 
(you did right to modify the button though - it should indeed have the ID as value)
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Mon Oct 28, 2013 10:32 am

Its working, but now, i need print 2 mikrotik router in the same script.
i add a new connection:
$client = new RouterOS\Client('','admin','admin');
$client2 = new RouterOS\Client('xxx.xxx.xxx.xxx','admin','admin');
But now, i dont know how to continue the script..:S

Other question, how can i sort by name the table?
Im trying with ksort but doesnt work :S
ksort($ppps);
ksort($activeInterfaces);
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Mon Oct 28, 2013 12:27 pm

Im trying combine the 2 arrays in one(ppps, and active connection) and print it, but it doesnt work..
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Mon Oct 28, 2013 6:50 pm

Other question, how can i sort by name the table?
Im trying with ksort but doesnt work :S
ksort() sorts by key, and the response collection has the reply's number as a key (first received has 0, second received has 1, etc.), not the name, and it's already sorted... so while ksort() could work (as in "be called normally and achieve what it's made to do"), it won't do anything (because "what it's made to do" is not what you want).

The previously mentioned solution for efficiently accessing an item by name will also let you export an array where the keys are name, though still ordered by how they were received. Calling ksort() on that array would produce what you want.

I've already implemented it in my develop branch, but there are a few more kinks I'd like to iron out before I release it (Notably: Tomorrow, when you decide you want to sort by a comment or whatever, where duplicates are possible - make a way to sort while preserving duplicates, ideally allowing you to sort those by a secondary criteria). If you can't wait, get yourself Git, clone the develop branch, set up an autoloader, and register the "src" folder with it... yes, that's not exactly trivial to do/follow, I know... dev releases are not intended to be easily usable.
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Mon Nov 04, 2013 10:53 am

Other question, how can i sort by name the table?
Im trying with ksort but doesnt work :S
ksort() sorts by key, and the response collection has the reply's number as a key (first received has 0, second received has 1, etc.), not the name, and it's already sorted... so while ksort() could work (as in "be called normally and achieve what it's made to do"), it won't do anything (because "what it's made to do" is not what you want).

The previously mentioned solution for efficiently accessing an item by name will also let you export an array where the keys are name, though still ordered by how they were received. Calling ksort() on that array would produce what you want.

I've already implemented it in my develop branch, but there are a few more kinks I'd like to iron out before I release it (Notably: Tomorrow, when you decide you want to sort by a comment or whatever, where duplicates are possible - make a way to sort while preserving duplicates, ideally allowing you to sort those by a secondary criteria). If you can't wait, get yourself Git, clone the develop branch, set up an autoloader, and register the "src" folder with it... yes, that's not exactly trivial to do/follow, I know... dev releases are not intended to be easily usable.

Hi again, i can sort the table with Datatables(jQuery) and works good, and i could test the restart button, and it not working :S

I'm using the same script:

php code

<html>
<head>
        <title>Listado ByWifi</title>
        <meta http-equiv="Content-Type" content="text/html; charet=UTF-8">
        <link href="css/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css">
        <link href="css/jquery.datatables.css" rel="stylesheet" type="text/css">
</head>

<body>

<?php

use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

//Conexion a Mikrotik
							//IP MIKROTIK	//Usuario	//Password
$clientcabarga = new RouterOS\Client('192.168.150.154', 'admin', '');




//Reiniciar PPP

$remove=new RouterOs\Request("/ppp/active/remove");
$remove->setArgument('numbers', $itemID); 

?>	
<!---Listado PPP--->
<form action='' method='POST' id='form'>	
<table id="mi_tabla" border='2' bordercolor='000000'>

<thead>
<tr bgcolor='#D8D8D8'>
	<th>Nombre</th>
	<th>Servicio</th>
	<th>Tiempo Activo</th>
	<th>Direccion</th>
	<th>Reiniciar</th>
</tr>
</thead>
<tbody>
<?php

$ppps = $clientcabarga->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'));
}

$activeInterfaces = $clientcabarga->sendSync(new RouterOS\Request('/interface/pppoe-server/print', $interfaceQuery))->getAllOfType(RouterOS\Response::TYPE_DATA)->toArray();


foreach ($ppps as $ppp) {
  $id = $ppp('.id');
  $service = '';
  foreach ($activeInterfaces as $index => $pppInterface) {
    if ($pppInterface('name') === $ppp('name')) {
      $service = $pppInterface('service');
      break;
    }
 }
  echo "<tr>";
 echo "<td>". $ppp('name') ."</td>";
  echo "<td>" . $service . "</td>";
  echo "<td>" . $ppp('uptime'). "</td>";
  echo "<td>". $ppp('address') ."</td>"; 
  echo "<td><button type='submit' value='{$id}' name='act[remove]'>Reiniciar</button></td>
  </tr>";
} 



?>	


		
</tbody></table></form>		
<!---DATATABLE// Ordenar y filtrar --->	
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.datatables.min.js"></script>
        <script type="text/javascript" language="JavaScript">
        $(document).ready( function() {
			$('#mi_tabla').dataTable();
		} );
</script>
</body>
</html>
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Mon Nov 04, 2013 4:16 pm

Well, you have the request here
//Reiniciar PPP

$remove=new RouterOs\Request("/ppp/active/remove");
$remove->setArgument('numbers', $itemID);
 
but you're never sending it.

Also, you'll notice that as it stands, you're ALWAYS preparing this request, as opposed to only when the reset button was pressed.

Check out your firewall form - the isset($_POST['act']).... the foreach() part... they're both missing.
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Mon Nov 04, 2013 4:47 pm

Well, you have the request here
//Reiniciar PPP

$remove=new RouterOs\Request("/ppp/active/remove");
$remove->setArgument('numbers', $itemID);
 
but you're never sending it.

Also, you'll notice that as it stands, you're ALWAYS preparing this request, as opposed to only when the reset button was pressed.

Check out your firewall form - the isset($_POST['act']).... the foreach() part... they're both missing.

Hmm that code is a bit hard to understand :S

I upgrade my code, but still not working:

php code

//Reiniciar PPP
				
		if (isset($_POST['act'])) {
    foreach ($_POST['act'] as $act => $itemID) {
        //Limit the action only to known ones, for security's sake
        if (in_array($act,'remove')) 
		{		
			$remove=new RouterOs\Request("/ppp/active/remove");
			$remove->setArgument('numbers', $itemID);
}
}
}
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Mon Nov 04, 2013 4:52 pm

in_array() checks against an array, not a string, so change
if (in_array($act,'remove'))  
to
if (in_array($act, array('remove')))  

To debug errors like this more quickly and easily, you may want to enable error display. Open up your php.ini file, look for "display_errors", and change that line from "Off" to "On". Also, find "error_reporting", and change it to "E_ALL | E_STRICT" from whatever else it has.
Hmm that code is a bit hard to understand :S
Look at it closely, and try to explain what it does (and how) in your own words. Use metaphors if you want to... I'll do my best to correct you at the points where you're too far off, perhaps by following your metaphors (if any).
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Tue Nov 05, 2013 9:56 am

in_array() checks against an array, not a string, so change
if (in_array($act,'remove'))  
to
if (in_array($act, array('remove')))  

To debug errors like this more quickly and easily, you may want to enable error display. Open up your php.ini file, look for "display_errors", and change that line from "Off" to "On". Also, find "error_reporting", and change it to "E_ALL | E_STRICT" from whatever else it has.
Hmm that code is a bit hard to understand :S
Look at it closely, and try to explain what it does (and how) in your own words. Use metaphors if you want to... I'll do my best to correct you at the points where you're too far off, perhaps by following your metaphors (if any).
Warning: in_array() expects parameter 2 to be array, string given in C:\wamp\www\tabla\tabsmikrotik.php on line 37

I dont find the problem :S

P.D Oops, my fault i have a wrong code, and i changed to
if (isset($_POST['act'])) 
{
	foreach ($_POST['act'] as $act => $itemID) 
	{
		if (in_array($act, array('remove')))   
			{		
				$remove=new RouterOs\Request("/ppp/active/{$act}");
				$remove->setArgument('numbers', $itemID);
			}
	}
}

But it still not working.. :(
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: print ppp active connections and interface list PEAR2 PH

Tue Nov 05, 2013 2:38 pm

Like I said, you're never sending the request... same as with your other recent topic.

So:
if (isset($_POST['act'])) 
{
   foreach ($_POST['act'] as $act => $itemID) 
   {
      if (in_array($act, array('remove')))   
         {      
            $remove=new RouterOs\Request("/ppp/active/{$act}");
            $remove->setArgument('numbers', $itemID);

            $client->sendSync($remove);
         }
   }
} 
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: print ppp active connections and interface list PEAR2 PH

Tue Nov 05, 2013 4:33 pm

Like I said, you're never sending the request... same as with your other recent topic.

So:
if (isset($_POST['act'])) 
{
   foreach ($_POST['act'] as $act => $itemID) 
   {
      if (in_array($act, array('remove')))   
         {      
            $remove=new RouterOs\Request("/ppp/active/{$act}");
            $remove->setArgument('numbers', $itemID);

            $client->sendSync($remove);
         }
   }
} 
OMG, my bad, i put the $client->sendSync($remove); on Error Message code, and i thought that code was to send me a error..Im sleeping.. :(
 
Tarif0011
just joined
Posts: 1
Joined: Thu Jun 16, 2022 10:41 am

Re: print ppp active connections and interface list PEAR2 PH

Thu Jun 16, 2022 11:24 am

hi mi new here to learn something new
Vyrtu sir can you please share your last update code with me
i also need how to show active user lists using php
other forum member also know how to show active user lists using php please try to help me

Who is online

Users browsing this forum: No registered users and 12 guests