Run scripts from API

I have problems with run a script in my rb.
I have a RB112 with ROS v5.2
I use web server “wampserver” in windows xp sp3
I use API PHP class and


<?php require('routeros_api.class.php'); $API = new routeros_api(); $API->debug = true; if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) { $API->write('/system/script/run'); $API->write('=.id=miapi'); $READ = $API->read(false); $API->disconnect(); } ?>

This connect with my rb12 and after disconnect but not execute the script “miapi”.
Thanks for your help

$API->write(‘/system/script/run’);
$API->write(‘=.id=miapi’);

>

You are trying the run the script using its .id, but passing the script name as the parameter.

run:

```text
:put [/system script find name=miapi]

from the terminal and use the id returned in your api write.
It should be something like *x

More work is to find the id based on name using:

$API->write('/system/script/getall\n?name=miapi\n=.proplist=.id');

This will return the id of the script “miapi” and then you can use that id in a second api call to actually run the script.

p.s. in my version of api write explodes newlines.

I resolved my problem with this code:

<?php require('routeros_api.class.php'); $API = new routeros_api(); $API->debug = true; if ($API->connect('111.111.111.111', 'login', 'password')) { $API->write('/system/script/getall',false); $API->write('=.proplist=name',false); $API->write('?name=miapi'); $API->write('/system/script/run',false); $API->write("=.id=*2"); $API->read(false); $API->disconnect(); } ?>

This work, i try it with ros v5.2.
I hope that this code can help others persons.

It is good you got it to work.
Unless you left something out, this code is meaningless to your objective:

   $API->write('/system/script/getall',false);
   $API->write('=.proplist=name',false);
   $API->write('?name=miapi');

It requests all the script names where the name = “miapi”.
So ROS returns “miapi” if that script exists.
But your code does nothing with that return data.
You can delete those lines or comment them out so you can refer back.

This is what actually runs the script:

   $API->write('/system/script/run',false);
   $API->write("=.id=*2");

Also, keep in mind that the value of .id can change from device to device and also if you delete the script and re-add it. It all depends on where in the database sequence the script was added.

is possible get details such as config and wireless info from one mikrotik to another mikrotik…
by using scripts.
actually i have one gsm modem connected with the router.
im going to use sms receiving option to get mikrotik info to my mobile fone.
it is not possible to connect all router to seperate gsm modem.
give me a valid solution for it…
thanks in advance

You probably should’ve started a separate topic, but anyway…

In my opinion, the best thing to do is use the API to connect to both the router you want to take data from and the router you want to transfer data to. Last I checked, there isn’t a way to have the router initiate a script as soon as it receives an SMS, although you can check at regular intervals, and activate a transferring procedure then. You can use RouterOS’ scheduler that would activate the API syncing program OR you can simply set the API syncing program to run on schedule, and only connect to the other routers if an SMS has been received.

Hello everybody.

I want to write php script which is equivalent to this:

/system script run [find name="myscript"]

I mean, I want to execute script on routerboard, but I want to use name of script instead of its id. But I’m really new to ros api and php..
I’d like to ask for some help =) Is there any way to interpret the command above? Or I should parse the output of

$API->write('/system/script/getall',false);

to get id from it?


Thanks in advance
p.s. sry for my English =)

it is possible to use name field for .id attribute value

for example if you have script added with default name of “script1”

then running over API

/system/script/run
=.id=script1

will execute that named script

ok, first of all, thank you for reply.
I’ve try to do as you said, but nothing happens..

Here’s script:

<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('iphere', 'login', 'pass')) {

	$API->write('/system/script/run',false);
	$API->write("=.id=script1");


   $API->disconnect();

}

?>

If I’ll put “*06” (i meat id of script) instead of “script1”, I works just fine.. Am I doing something wrong?

well, i tested that on 5.20 and 5.21 (testing build) and it did execute the script via name. You will not see any results in API of script running, except script does something that you can check, like, set comment field.

Also, if you disconnect too fast, you might terminate session before command is executed. because router can accept commands one after another and return output when done.

So read output at the end.

Please, how can I execute the following using RouterOS PHP API:

[admin@MikroTik] ip hotspot user> add name=ex password=ex \
\... mac-address=01:23:45:67:89:AB limit-uptime=1h

Already answered in that other topic.

Work for me

<?php
ini_set("default_charset", "UTF-8");
header('Content-Type: application/json; charset=utf-8');
require('./routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = false;
if ($API->connect('192.168.1.1', 'user', 'pass')) {
$API->write('/system/script/getall', false);
$API->write('=.proplist=.id',false);
$API->write('?name=wlan_on');
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
if(isset($ARRAY['0']) && isset($ARRAY['0']['.id'])){
	$API->write('/system/script/run',false);
	$API->write('=.id='. $ARRAY['0']['.id']);
	$READ = $API->read(false);
	//$ARRAY = $API->parseResponse($READ);
	echo '{"name":"wlan_on","state":"OK"}';
} else {
  	echo '{"name":"wlan_on","state":"ERROR"}';
}
//print_r($ARRAY);
$API->disconnect();
}