npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mhycy/routeros-client

v1.0.0

Published

RouterOS api client

Downloads

125

Readme

@mhycy/routeros-client

MikroTik RouterOS API client for node.js.

Install

$ npm install @mhycy/routeros-client

Usage

const RouterClient = require('@mhycy/routeros-client');

const USER = "admin";
const PASSWD = "test"

// connect options
const connectOptions = {
  host: '192.168.1.254',
  // port: 8728,
  // encoding: 'gbk',
  debug: true
};

// create client instance
const client = RouterClient.createClient(connectOptions);

// async request
(async function() {
  // login helper function
  await client.login(USER, PASSWD);

  // login use command builder
  await client.command("/login").setAttrs({
    name: USER,
    password: PASSWD
  }).send();
  
  // get interfaces that 'type' attribute equal 'pppoe-out'
  await client.command("/interface/print")
          .equal("type", "pppoe-out")
          .send();

  client.close();
})();

API

module.createClient(options)

Helper for new module.Client(options)

Class: module.CommandBuilder

Command sentences chainable query builder.

Query word see offical wiki: Mikrotik-Wiki-API

Example:

let sentences = new CommandBuilder("/interface/print")
                      .equal('type', 'pppoe-out')
                      .get();
/* sentences
[
  '/interface/print',
  '?type=pppoe-out'
]
*/

new mooduel.CommandBuilder(command, [callback])

  • command <string> command word. Example: /interface/print
  • callback <Function> use for .get() method. Defualt: (sentences) => { return sentences; }
    • sentences <array> sentences array.

Create a new CommandBuilder object.

Example:

new CommandBuilder('/login', (sentences) => {
  console.log(sentences);
})
.setAttrs({
  name: 'admin',
  password: 'test'
})
.get();

// console.log output
// [
//   '/login',
//   '=name=admin',
//   '=password=test',
// ]

CommandBuilder.addAttr(name, value)

  • name <string> attribute name.
  • value <string> attributes value.

Add an attribute value to exists attributes object.

Example:

let sentences = new CommandBuilder('/login')
      .setAttr(name, 'admin')
      .setAttr(password, 'test')
      .get();

CommandBuilder.setAttrs(attrs)

  • attrs <object> attributes object.

Overwrite attributes object.

Example:

let sentences = new CommandBuilder('/login')
      .setAttrs({
        name: 'admin',
        password: 'test'
      })
      .get();

CommandBuilder.exists(name)

  • name <string> property name.

Query word ?name, check property name exists.

CommandBuilder.doesntExists(name)

  • name <string> property name.

Query word ?-name, check property name doesn't exists.

CommandBuilder.equal(name, value)

  • name <string> property name.
  • value <string> attributes value.

Query word ?name=value, check property name has value equal to value.

CommandBuilder.lessThan(name, value)

  • name <string> property name.
  • value <string> attributes value.

Query word ?<name=value, check property name has value less than value.

CommandBuilder.greaterThan(name, value)

  • name <string> property name.
  • value <string> attributes value.

Query word ?>name=value, check property name has value greater than value.

CommandBuilder.operation(operation)

  • operation <string> operation word < |, &, !, . > .

Operation word see Mikrotik-Wiki-API.

Class: module.Client

new module.Client(options)

  • options
    • host <string> Router hostname or ip
    • port <number> api port, doesn't support ssl. Default: 8728.
    • encoding <string> sentences enc/dec encoding, it equal windows environment encode. (MikroTik WinBox used it) Default: gbk.
    • debug <boolean> if true print debug infomation to console. Default: false.

Create a new Client object.

Event: 'receive'

  • <object> sentences decode object.
// '!done'
{
  status: true
}

// '!re'
// ......
// '!re'
// ......
{
  'status': true,
  'replies': [
    {
      '.id': '*9',
      name: 'pppoe-out1',
      type: 'pppoe-out',
      'link-downs': '0',
      'rx-byte': '0',
      'tx-byte': '0',
      'rx-packet': '0',
      'tx-packet': '0',
      'rx-drop': '0',
      'tx-drop': '0',
      'tx-queue-drop': '0',
      'rx-error': '0',
      'tx-error': '0',
      'fp-rx-byte': '0',
      'fp-tx-byte': '0',
      'fp-rx-packet': '0',
      'fp-tx-packet': '0',
      running: 'false',
      'tx-queue-drop': '0',
      'rx-error': '0',
      'tx-error': '0',
      'fp-rx-byte': '0',
      'fp-tx-byte': '0',
      'fp-rx-packet': '0',
      'fp-tx-packet': '0',
      running: 'true',
      disabled: 'false',
      comment: 'ChinaNet 500M'
    }
  ]
}

// '!trap'
// '=message=invalid user name or password (6)'
{
  status: false,
  message: 'invalid user name or password (6)'
}

// '!fatal',
// 'not logged in',
{
  status: false,
  message: 'not logged in'
}

Client.socket

<net.Socket> instance.

Client.close()

Close Client.socket. Call <net.Socket.end()> to close.

Client.sendSentences(sentences)

  • sentences <array> sentences array.

Direct write a sentences, you need set listener to listen Event::receive by your self.

Client.sendSentencesAsync(sentences)

  • sentences <array> sentences array.

Return: <Promise>

Promise resolve an object. see: Usage

Client.command(command)

  • command <string> command words. Example: /login or /interface/print

Return: <CommandBuilder>

Return chainable query builder. CommandBuilder.get() method will send sentences request. (Use: Client.sendSentencesSync())

Client.login(name, password)

  • name <string> user name.
  • password <string> user password.

Return: <Promise>

Promise resolve an object. see: Usage

Client.setLogger(logger)

  • logger <object> new logger instance.

Logger instance need implementing some method. see src/logger.js source code.

module.Logger

Module logger, export api see src/logger.js source code.

module.Utils

Module utilities, export api see src/utils.js source code.