Community discussions

MikroTik App
 
amirreza
just joined
Topic Author
Posts: 14
Joined: Sat Feb 04, 2012 2:28 pm

Remove or Disable in API with PHP

Sat Feb 04, 2012 3:12 pm

Dear All.
<?php
//config
//Enter your mikrotik router IP, Username and password here

$serverip="192.168.1.1";
$username="admin";
$password="123456";
//***********************
require('api.php');
$address = $_SERVER['REMOTE_ADDR'];
$usercomment  = $_SERVER['QUERY_STRING'];
$API = new routeros_api();
$API->debug = false;
if ($API->connect($serverip, $username,$password)) {
	$API->comm("/ip/socks/access/add", array(
          "src-address" => "$address",
          "comment"  => "{$usercomment}",
          "action" => "allow",
));

I am using this API code for add IP in SOCKS.

How can change this code for Remove or Disable exmaple IP 3.3.3.3 ?

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

Re: Remove or Disable in API with PHP

Sat Feb 04, 2012 4:01 pm

Make a print request with a query that matches the item you want to remove/disable/set. Part of the response will be the ".id" of that item. Pass that to the "numbers" argument of a new remove/disable/set command.
 
amirreza
just joined
Topic Author
Posts: 14
Joined: Sat Feb 04, 2012 2:28 pm

Re: Remove or Disable in API with PHP

Sat Feb 04, 2012 11:29 pm

Make a print request with a query that matches the item you want to remove/disable/set. Part of the response will be the ".id" of that item. Pass that to the "numbers" argument of a new remove/disable/set command.
how can I edit this code with print request?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Remove or Disable in API with PHP

Sun Feb 05, 2012 12:19 am

With my client, it would look like:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('192.168.1.1', 'admin', '123456');

$id = $client->sendSync(
    new Request('/ip/socks/access/print .proplist=.id', null, Query::where('address', $_SERVER['REMOTE_ADDR']))
)->getArgument('.id');

$disableRequest = new Request('/ip/socks/access/disable');

$client->sendSync($disableRequest->setArgument('numbers', $id));
 
And with your current client... it would basically be two comm() calls.
 
amirreza
just joined
Topic Author
Posts: 14
Joined: Sat Feb 04, 2012 2:28 pm

Re: Remove or Disable in API with PHP

Tue Feb 07, 2012 4:12 pm

With my client, it would look like:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('192.168.1.1', 'admin', '123456');

$id = $client->sendSync(
    new Request('/ip/socks/access/print .proplist=.id', null, Query::where('address', $_SERVER['REMOTE_ADDR']))
)->getArgument('.id');

$disableRequest = new Request('/ip/socks/access/disable');

$client->sendSync($disableRequest->setArgument('numbers', $id));
 
And with your current client... it would basically be two comm() calls.

tnx but This code does not work.
i upload PEAR2_Net_RouterOS in CPanel Host and use this code.
in this code i don't need REMOTE_ADDR, and I just determine use a PHP variable $ for, set the ID.

example:
$delete_id="3";
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Remove or Disable in API with PHP

Tue Feb 07, 2012 4:50 pm

You need an ID. Using a number won't work. The "numbers" argument in MikroTik's API, ironically enough, doesn't accept numbers as its Winbox counterpart. You MUST use an ID, and you can get the correct ID with a print request, as in the example above.

If you want to disable a specifc IP (as opposed to $_SERVER['REMOTE_ADDR']), you can hardcode the IP itself onto the query, i.e.
$id = $client->sendSync(
    new Request('/ip/socks/access/print .proplist=.id', null, Query::where('address', '3.3.3.3'))
)->getArgument('.id');
 

Who is online

Users browsing this forum: No registered users and 4 guests