Community discussions

 
nacho0vp0vm
just joined
Topic Author
Posts: 2
Joined: Wed May 16, 2018 3:47 pm

Multiple commands through API

Wed May 16, 2018 3:56 pm

Hi all,

We would like to add addresses to the firewall blacklist as fast as possible to hinder and or prevent actual attacks. For this we use the api. However as you can sends commands only one-by-one this is relatively slow. One solution to speed things up is to combine commands into one api-call. This is possible through the command line. So the questions is: is it also possible to send multiple commands in one api-call and if so: how?

Kind regards,
Nacho
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2379
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Multiple commands through API  [SOLVED]

Wed May 16, 2018 7:01 pm

Surrounding multiple commands on the command line with "{" and "}" and/or separating them with ";" isn't really combining them into one. The router still treats them as independent commands... I mean, you can't really combine commands into one call even on CLI, and thus not on API.

You could make one call to "/system/script/add" to add a temp script, and a second call to "/system/script/run" to run that script. Within the script, you'd add all of your firewall rules or whatever else normally takes too long on the API, and remove the script itself when you're done. Thus, you'd combine many calls into just two (or three if you make the script removal not part of the temp script).

OR you could use an API client that lets you make multiple API calls without waiting for their replies (with tags). This lets you send multiple add requests all at once. They'll still only be committed to the router after you get the corresponding reply, but because you already sent the "next" reply before waiting for the first one's reply, the operation will be faster.

My API client implements both possibilities with Util::exec() and Client::sendAsync(), respectively. Not all clients (in general) support the second option, but the first can be easily made with any API client, even if it isn't as convenient as a simple method call in the language.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
nacho0vp0vm
just joined
Topic Author
Posts: 2
Joined: Wed May 16, 2018 3:47 pm

Re: Multiple commands through API

Thu May 17, 2018 10:46 am

First of all: thanks for the suggestions. I already had 'dissected' the (python) API so sending apart from receiving was easy. However, it seems that a command is executed after the response is read. This calls for async response processing, making things a bit more complex but that's fine with me. I already was thinking twisted so here we go.
Concerning the combination of commands: it's a matter of perspective. From my pov on the cli I enter 1 line containing several commands which are executed after hitting return. I wondered if the same could happen through the api as sending one (long)line is much faster then lots of short lines, but I guess this is not possible.

Kind regards,
Nacho

Who is online

Users browsing this forum: kcarhc and 6 guests