mikro-client
v0.5.3
Published
Simple library using socket API to connect and control Mikrotik devices in JavaScript or TypeScript. Allows for easy automation of Mikrotik device management and configuration tasks.
Downloads
123
Maintainers
Keywords
Readme
mikro-client
This library, written in TypeScript and also compatible with JavaScript, provides a simple connection via TCP sockets to send and receive command-line commands from MikroTik. With this library, you can easily and quickly control and get information from a MikroTik device.
version documentation in:
- English
- Spanish
Installation
npm install mikro-client
or
yarn add mikro-client
Usage
To use the class, you must first import it into your project:
import { MikroClient } from 'mikro-client'
Then, you can create a new instance of the class by providing the necessary configuration options:
const options = {
host: 'XXX.XXX.XXX.XXX',
port: XXXX,
username: 'xxxxx',
password: 'xxxxx',
timeout: 5000,
}
const mikro = new MikroClient(options)
Once you have an instance of the class, you can use the "talk" method to send MikroTik commands to the device and receive the response:
const response = await mikro
.talk(['/interface/print'])
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
You can also specify the type of response you want to receive using the optional "type" argument:
const response = await mikro
.talk(['/interface/print'], 'object')
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
Examples
Here are some examples of MikroTik commands that you can send using the "talk" method:
- Get network interface information:
const response = await mikro
.talk(['/interface/print'])
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
- Add a firewall rule:
mikro
.talk([
'/ip/firewall/filter/add',
'=chain=forward',
'=protocol=tcp',
'=dst-port=80',
'=action=accept',
])
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
- Get connected user information:
mikro
.talk(['/ip/hotspot/active/print'])
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
- Create a hotspot user:
mikro
.talk([
'/ip/hotspot/user/add',
'=name=example',
'=password=example',
'=profile=default',
])
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
})
Information
For more information on the MikroTik commands that you can send using the MikroApi class, we recommend consulting the MikroTik documentation, which is available at the following link:
- API - RouterOS - MikroTik Documentation
On this page, you will find a complete list of available MikroTik commands and their respective parameters. You will also find examples of how to use these commands in the MikroTik command line and API.
I hope this helps you get more information on MikroTik commands and how to use them with the MikroApi class. If you have any additional questions, don't hesitate to ask.
License
- MIT
Created by jjjjose - 2022