Community discussions

MikroTik App
 
areeb111
just joined
Topic Author
Posts: 15
Joined: Mon Sep 07, 2009 1:58 pm

PHP API Question

Fri Jan 13, 2012 7:29 pm

Dear All,
<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('172.17.10.2', 'user', 'pass')) {

$API->comm("/ppp/secret/disable", array(
          "numbers"     => "1",
));

   $API->disconnect();

}

?>
I am using this API code but why it's not working please tell me how to use it correctly, i have added users and it works like a charm
;)

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

Re: PHP API Question

Fri Jan 13, 2012 7:38 pm

The numbers argument doesn't use values equivalent to those you see in Winbox. It instead uses IDs.

You need to get the ID of the item you're about to disable/remove/set/unset/etc., and use that as the value of "numbers".
 
areeb111
just joined
Topic Author
Posts: 15
Joined: Mon Sep 07, 2009 1:58 pm

Re: PHP API Question

Fri Jan 13, 2012 9:49 pm

The numbers argument doesn't use values equivalent to those you see in Winbox. It instead uses IDs.

You need to get the ID of the item you're about to disable/remove/set/unset/etc., and use that as the value of "numbers".
Yes, Of course i am using the specific ID which is used in Terminal.
I have (4) secrets in ppp/secrets

So when i do this command in Terminal

ppp secret disable 1

It works,
But in API nothing works. and it displays this text.
Connection attempt #1 to 172.17.10.2:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 39]!done >>> [37/37] bytes read. >>> [37, 1]=ret=0b3a981372f63a499ca228496a268d87 <<< [6] /login <<< [10] =name=wave <<< [44] =response=002f501339c78070a0868f5f73cf5d8a33 >>> [5/5] bytes read. >>> [5, 1]!done Connected... <<< [19] /ppp/secret/disable <<< [10] =numbers=1 >>> [5/5] bytes read. >>> [5, 42]!trap >>> [11/11] bytes read. >>> [11, 30]=category=0 >>> [21/21] bytes read. >>> [21, 8]=message=no such item >>> [5/5] bytes read. >>> [5, 1]!done Disconnected...
What do u think about this ?
 
areeb111
just joined
Topic Author
Posts: 15
Joined: Mon Sep 07, 2009 1:58 pm

Re: PHP API Question

Fri Jan 13, 2012 9:56 pm

And if there is another ID how can i view the item's ID ?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP API Question

Sat Jan 14, 2012 2:09 pm

Yes, there is another ID (in the form of a "*" followed by a hex number).

You get it by using "print", combined with a query that targets the item(s) you wish to later target for disabling/removal/setting/etc.

See this and this topic for examples.
 
alphahawk
Member Candidate
Member Candidate
Posts: 101
Joined: Fri Mar 28, 2008 6:40 pm

Re: PHP API Question

Tue Jan 17, 2012 1:05 am

Your looking for something like this. Its rough and I probably screwed something up in there but should give you the general idea
<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('172.17.10.2', 'user', 'pass')) {

$API->write('/ppp/secret/getall');
$read=$API->read(false);
$ARRAY=$API->parse_response($read);
print_r($ARRAY);

$API->write('/ppp/secret/disable',false);
$API->write('=.id=' . $ARRAY['.id'],false);
$API->write('=numbers=1');


   $API->disconnect();

}

?>
 
areeb111
just joined
Topic Author
Posts: 15
Joined: Mon Sep 07, 2009 1:58 pm

Re: PHP API Question

Thu Jan 19, 2012 11:38 am

Your looking for something like this. Its rough and I probably screwed something up in there but should give you the general idea
<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('172.17.10.2', 'user', 'pass')) {

$API->write('/ppp/secret/getall');
$read=$API->read(false);
$ARRAY=$API->parse_response($read);
print_r($ARRAY);

$API->write('/ppp/secret/disable',false);
$API->write('=.id=' . $ARRAY['.id'],false);
$API->write('=numbers=1');


   $API->disconnect();

}

?>

Thank you, but i have tried this code and it's not working.

Please could you do me favor i want to run this command via PHP API
/system script run 0
I will be very appreciated.
Many thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP API Question

Thu Jan 19, 2012 2:03 pm

It's the same deal - print the ID (the internal ID, not the Winbox # column!) of the script you'll be running, and pass it to the run command.

With the client from my signature (dirty installation: download it, extract the "src" folder somewhere, create the following file in the same folder), you can disable an account with:
<?php
namespace PEAR2\Net\RouterOS;//This must remain at the top of your PHP file!
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('172.17.10.2', 'user', 'pass'));

//The key bit... getting the correct ID by printing it
$id = $client->sendSync(new Request('/ppp/secret/print .proplist=.id', null, Query::where('name', 'areeb111')))->getArgument('.id');

$disableRequest = new Request('/ppp/secret/disable');
$client->sendSync($disableRequest->setArgument('numbers', $id));
//And you're done.
 
(replacing the "areeb111" part with the actual username you want to disable)

To run a script, the code above will be the same, except that "/ppp/secret" will be replaced with "/system/script", and "/disable" will be "/run" instead.

Try it!

Who is online

Users browsing this forum: No registered users and 12 guests