Passing to Paramater to script via API

Hi,

Cant work out if this is possible…

We currently run an API connection to our HotSpots from our managment system allowing our support team to manually bypass a users mac address if for some reason they cant see the portal.

Basically, the API does this:

Adds a mangle rule to mark the bypased users IP address
Adds a Queue to limit the bypassed users traffic in line with what we want them to have
Bypasses the user in the /hotspot ip-bindings page.


Works a treat, however, I would like to move the logic to the Mikrotik device as a script, and just call the script via the API .

Is it possible to pass a parameter into a script though, as Ive never done that?

Not exactly…

Even in scripting alone, the only way to run a script with parameters is to parse the script into a variable (or define the script as the value of a variable, but either way…), meaning you can’t exactly “store” parameterized scripts.

You could make a script that defines all of your parameterized scripts into global variables, and you set that script to run on startup, effectively achieving a similar effect.

Once you have THAT, with the API, you can run scripts from “/system script”. You can use the API to add a temporary script, which contains only a single line where you call the variable, along with the parameters for the script stored in it. To prevent code injection, you’d need to escape the values properly.

With my client, all of that is as easy as

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.0.1', 'username', 'password'));
$util->exec(
    '$script param1=$param1val param2=$param2val',
    array(
        'param1val' => $_POST['p1'],
        'param2val' => $_POST['p2']
    )
); 

(where $script is the previously parsed source of the script in which $param1 and $param2 are the parameters)

With most other clients (not just PHP ones), it’s a little bit more difficult, particularly the part about escaping the value. You can see the source for that here if you need to rewrite it for a different language (the key is the constant escape sequence for arbitrary characters).

Hi

Thanks for taking the time to leave such a detailed post, i;ll be looking into it next week when the workload dies down a bit. If only Mikrotik would release a ‘login user’ API call.