Im trying to run the command using the API
Doing this in the Mikrotik API studio:
/ip/hotspot/host/remove
=numbers=*8
*8 being the latest session
works 100%
even from a terminal works 100%
but this:
<?php
require('api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect('192.168.1.50', 'xxxx', 'xxxx')) {
$API->write('/ip/hotspot/host/remove');
$API->write('=numbers=*8);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
}
comes back with
Array ( [!trap] => Array ( [0] => Array ( [message] => no such command prefix ) ) )
any ideas ?
other commands work ok
$API->write('=numbers=*>
> ;
Aren’t you missing a closing single quotation mark here?
With that client, you need to explicitly signify you’re not ready to send the sentence by specifying false as the second argument to write(). Try it like so:
<?php
require('api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect('192.168.1.50', 'xxxx', 'xxxx')) {
$API->write('/ip/hotspot/host/remove', false);
$API->write('=numbers=*8');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
}
(also fixed the above mentioned syntax error)
P.S. I’d love to know what made you choose that client over the other one.
<?php
require('api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect('192.168.1.50', 'xxxx', 'xxxx')) {
$API->write('/ip/hotspot/host/remove', false);
$API->write('=numbers=*8');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
}
perfect worked great !
next question is it possible to do this based on a mac id ?
with your client would it be easier to remove a session based on a users mac id ?
I thought you’d never ask
.
With either client, you need to send a query with a print request to get the ID, and pass it to the remove command. I think that’s done easier with my client, but I’m biased.
Here’s an example:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
$client = new Client('192.168.1.50', 'xxxx', 'xxxx');
$id = $client->sendSync(
new Request('/ip hotspot host print .proplist=.id', Query::where('mac-address', '00:00:00:00:00:01'))
)->getArgument('.id');
//$id now contains the ID of the entry we're targeting
$removeRequest = new Request('/ip/hotspot/host/remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest);
As it stands, it removes just the first match. With some minor modifications, you can make it delete all matches. The code can be shortened even further than this (especially if you have PHP 5.4), but I’ve kept it more verbose so that you know what’s going on.
See this page for more examples.