Skip to content

A Lua library for talking to the Mikrotik RouterOS API

License

Notifications You must be signed in to change notification settings

karolba/lua-mikrotik

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-mikrotik

A lightweight lua library for talking to the Mikrotik RouterOS API.

Requirements:

Documentation:

For documentation see doc/api.md.

Examples:

Starting a RouterOS script from /system/script by name synchronously:

local Mikrotik = require 'Mikrotik'

local function runScript(mt, scriptname)
    assert(mt:sendSentence({ '/system/script/print', '?name=' .. scriptname, '=.proplist=.id' }))

    local message = assert(mt:readSentence())
    assert(message.type == '!re')
    assert(mt:readSentence().type == '!done')

    assert(mt:sendSentence({ '/system/script/run', '=number=' .. message['=.id']}))
    assert(mt:readSentence().type == '!done')

    print('OK!')
end

local mt = assert(Mikrotik:create('192.168.88.1'))
assert(mt:login('login', 'password'), 'Failed login')

runScript(mt, 'example-routeros-script-name')

The same result can be accomplished by using "tagged sentences" and callbacks:

local Mikrotik = require 'Mikrotik'

local function runScript(mt, scriptname)
    assert(mt:sendSentence({ '/system/script/print', '?name=' .. scriptname, '=.proplist=.id' }, function(res)
        if res.type == '!re' then
            assert(mt:sendSentence({ '/system/script/run', '=number=' .. res['=.id'] }, function(res)
                if res.type == '!done' then
                    print('OK!')
                end
            end))
        end
    end))
end

local mt = assert(Mikrotik:create('192.168.88.1'))
assert(mt:login('login', 'password'), 'Failed login')

runScript(mt, 'example-routeros-script-name')

assert(mt:wait())

About

A Lua library for talking to the Mikrotik RouterOS API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages