Use attached code to create a user library named mikrotik.
Make sure that your router is running at least v6.43.
It is recommended to create a separate user for api access, possibly with limited read-only access if you don't need to change anything on the router remotely.
if mt then
res, err = mt:login('api', '123456') -- user api, pass 123456
if res then
-- get wireless registration table
res, err = mt:readlist('/interface/wireless/registration-table/print')
log(res)
else
log('login failed', tostring(err))
end
mt:close()
else
log('connect failed', tostring(err))
end
Unify API is done via HTTP requests. You need to send one request for login, get a cookie value from it and pass this cookie to any other requests after that.
Here's an example that can be used as a starting point: https://forum.logicmachine.net/showthrea...3#pid18233
The difference is that this example uses standard POST whereas Unify communicates via raw POST / JSON. Like this:
20.10.2020, 17:16 (This post was last modified: 20.10.2020, 17:16 by gjniewenhuijse.)
(20.10.2020, 06:57)admin Wrote: Use attached code to create a user library named mikrotik.
Make sure that your router is running at least v6.43.
It is recommended to create a separate user for api access, possibly with limited read-only access if you don't need to change anything on the router remotely.
if mt then
res, err = mt:login('api', '123456') -- user api, pass 123456
if res then
-- get wireless registration table
res, err = mt:readlist('/interface/wireless/registration-table/print')
log(res)
else
log('login failed', tostring(err))
end
mt:close()
else
log('connect failed', tostring(err))
end
-- get wireless registration table if you use capsman
/caps-man/registration-table/print
07.12.2020, 13:11 (This post was last modified: 07.12.2020, 13:30 by DGrandes.)
Thanks so much!! It works
I have another problem with api:
I´ve created an script in Mikrotik that works when I execute it directly from Mikrotik but if I execute it from LM Api, it runs but only the first line:
The default timeout is 5 seconds, you can try increasing it by editing the library code. Why do you need this script on Mikrotik? LM can run the same HTTP queries.
07.12.2020, 15:03 (This post was last modified: 07.12.2020, 15:08 by DGrandes.)
Because I´m trying doing bandwith test and get the result from LM but when I send this comand from LM: "/tool bandwidth-test 38.104.52.187 duration=10s direction=both protocol=tcp user=btest pass=btest" give me the result in the moment, not final result.
Because of this, i´m trying doing the same with an script. I run Bandwidth test script from LM and this script response me with Bandwidth results. But doesn´t work neither.
For bandwidth test you need to use readlist because MT will report test result every second (this also means that the timeout value should not be changed). The last element of res table will contain the final result.
(09.12.2020, 07:19)admin Wrote: For bandwidth test you need to use readlist because MT will report test result every second (this also means that the timeout value should not be changed). The last element of res table will contain the final result.
(20.10.2020, 12:13)admin Wrote: Unify API is done via HTTP requests. You need to send one request for login, get a cookie value from it and pass this cookie to any other requests after that.
Here's an example that can be used as a starting point: https://forum.logicmachine.net/showthrea...3#pid18233
The difference is that this example uses standard POST whereas Unify communicates via raw POST / JSON. Like this:
Finally got around to get credentials and actually test this out. With the help from your example I managed to fetch a list over active clients. Thank you very much!
10.01.2021, 15:45 (This post was last modified: 10.01.2021, 15:55 by Igori.)
Hello!
I'm trying this method of connecting to a Mikrotik, I did everything according to the instructions. But I get the following error in the examle script:
"Resident script:1: attempt to index a boolean value
stack traceback:"
Example: how to disable list of Mikrotik access points (managed by capsman) with the comment "ap"
Code:
mt, err = require('user.mikrotik').new('192.168.0.11') -- capsman manager IP 192.168.0.11
if mt then
res, err = mt:login('api', 'Password123') -- user api, password Password123
if res then
list, err = mt:readlist('/caps-man/interface/print')
for _, item in ipairs(list) do
id = item['.id']
if id and item.comment == 'ap' then
r1, e1 = mt:send('/caps-man/interface/disable', '=.id=' .. id)
r2, e2 = mt:read()
-- log(id, r1, e1, r2, e2)
end
end
else
log('login failed', tostring(err))
end
mt:close()
else
log('connect failed', tostring(err))
end