JSPM

  • Created
  • Published
  • Downloads 1309
  • Score
    100M100P100Q12962F
  • License MIT

Mikrotik Routerboard RouterOS API for NodeJS

Package Exports

  • node-routeros

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-routeros) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Description

This is a Mikrotik Routerboard API written in Typescript for nodejs, can be either used with plain javascript or imported on typescript projects.

This project is entirely based on George Joseph and Brandon Myers's work with mikronode, thank you very much.

Features

  • Connection and reconnection without destroying the object.
  • Change host, username and other parameters of the object without recreating it.
  • Based on promises.
  • You can choose to keep the connection alive if it gets idle.
  • Every command is async, but can be synced using the promises features.
  • Can pause, resume and stop streams (like what you get from /tool/torch).
  • Support languages with accents, keeping it consistent throughout winbox and api.
  • Support multiple languages for throwing errors. (Currently only brazilian portuguese and english)

Usage

npm install node-routeros --save

Documentation

Check the wiki for a complete documentation.

Examples

You can import in TypeScript using:

import { RouterOSAPI } from "node-routeros";

Adding an IP address to ether2, printing it, then removing it synchronously:

const RosApi = require("node-routeros").RouterOSAPI;

const conn = new RosApi({
    host: "192.168.88.1",
    user: "admin",
    password: ""
});

conn.connect().then(() => {
    // Connection successful

    // Let's add an IP address to ether2
    conn.write("/ip/address/add",[
        "=interface=ether2",
        "=address=192.168.90.1"
    ]).then((data) => {
        console.log("192.168.90.1 added to ether2!", data);
        
        // Added the ip address, let's print it
        return conn.write("/ip/address/print", ["?.id=" + data[0].ret]);
    }).then((data) => {
        console.log("Printing address info: ", data);
        
        // We got the address added, let's clean it up
        return conn.write("/ip/address/remove", ["?.id=" + data[0][".id"]]);
    }).then((data) => {
        console.log("192.168.90.1 as removed from ether2!", data);
        
        // The address was removed! We are done, let's close the connection
        conn.close();
    }).catch((err) => {
        // Oops, got an error
        console.log(err);
    });

}).catch((err) => {
    // Got an error while trying to connect
    console.log(err);
});

Listening data from /ip/torch and using pause/resume/stop feature:

const RosApi = require("node-routeros").RouterOSAPI;

const conn = new RosApi({
    host: "192.168.88.1",
    user: "admin"
    password: ""
});

conn.connect().then(() => {
    // Counter to trigger pause/resume/stop
    let i = 0;

    // The stream function returns a Stream object which can be used to pause/resume/stop the stream
    const addressStream = conn.stream(['/tool/torch', '=interface=ether1'], (error, packet) => {
        // If there is any error, the stream stops immediately
        if (!error) {
            console.log(packet);

            // Increment the counter
            i++;

            // if the counter hits 30, we stop the stream
            if (i === 30) {

                // Stopping the stream will return a promise
                addressStream.stop().then(() => {
                    console.log('should stop');
                    // Once stopped, you can't start it again
                    conn.close();
                }).catch((err) => {
                    console.log(err);
                });

            } else if (i % 5 === 0) {

                // If the counter is multiple of 5, we will pause it
                addressStream.pause().then(() => {
                    console.log('should be paused');

                    // And after it is paused, we resume after 3 seconds
                    setTimeout(() => {
                        addressStream.resume().then(() => {
                            console.log('should resume');
                        }).catch((err) => {
                            console.log(err);
                        });
                    }, 3000);

                }).catch((err) => {
                    console.log(err);
                });

            }

        }else{
            console.log(error);
        }
    });

}).catch((err) => {
    // Got an error while trying to connect
    console.log(err);
});

Testing

Currently, I am running tests though a VM with RouterOS CHR. I still haven't figured out a better way to do this.

TODO

  • More tests
  • CI and deployment to npm