Home

Awesome

Mikronode

Full-Featured asynchronous Mikrotik API interface for NodeJS.

    var MikroNode = require('mikronode');
    
    var device = new MikroNode('192.168.0.1');

    device.connect()
      .then(([login])=>{
        return login('username','password');
      })
      .then(function(conn) {

        var chan=conn.openChannel("addresses"); // open a named channel
        var chan2=conn.openChannel("firewall_connections",true); // open a named channel, turn on "closeOnDone"

        chan.write('/ip/address/print');

        chan.on('done',function(data) {

             // data is all of the sentences in an array.
             data.forEach(function(item) {
                console.log('Interface/IP: '+item.data.interface+"/"+item.data.address);
             });

             chan.close(); // close the channel. It is not autoclosed by default.
             conn.close(); // when closing connection, the socket is closed and program ends.

        });

        chan.write('/ip/firewall/print');

        chan.done.subscribe(function(data){

             // data is all of the sentences in an array.
             data.forEach(function(item) {
                var data = MikroNode.resultsToObj(item.data); // convert array of field items to object.
                console.log('Interface/IP: '+data.interface+"/"+data.address);
             });

        });

    });

Installation

Clone this repository into your node_modules directory.

Features

TODO

API

    // There are 2 ways to get resulting data from channels:
    // Using events, like earlier versions:
    //   data takes each sentence one at a time.
    //   done takes an entire result from last command.
    // Using Streams channel and connection provides access to several layers of them.
    //   Channels filter only data for that channel
    //      data: sentences that are data.
    //      trap: stream of traps. Useful for reading data streams using takeUntil(trapStream). Or for piping to notification on UI.
    //      bufferedSteam: when data response is "done" the buffered stream emits packets. Don't use this with a "listen" command.
    //      read: every sentence passes through this one.
    //   Connections also have streams, where they do not filter per channel:
    //      raw: The raw socket data. This emits buffers.
    //      sentence:each raw sentence is emitted from this stream
    //      read: the parsed sentences this is similar to the Channel read stream except does not filter by channel id.
    //      trap: all traps

Connection

Calling new MikroNode(host[,port,socketTimeout]) returns an object representing the device.

    var MikroNode = require('mikronode');
    var Device =new MikroNode(host,port);
    Device.connect().then(([login])=>login('admin','password')).then(function(conn) { 
        var chan=conn.openChannel();
    });

With the above code, the following is API description. conn is Connection object, chan is Channel object.

Channel

The following property/methods are available for channels:

Examples

Connect to a Mikrotik, and add an address to ether1

     var api = require('mikronode');

     var device = new api('192.168.0.1');
     device.connect().then(([login])=>login('admin','password')).then(function(conn) {

        var chan=conn.openChannel();

        chan.write('/ip/address/add',{'interface':'ether1','address':'192.168.1.1'});
        chan.on('trap',function(data) {
            console.log('Error setting IP: '+data);
        });
        chan.on('done',function(data) {
            console.log('IP Set.');
        });
        chan.close();
        conn.close();
     });

Writing the program for the example API conversation on the Mikrotik Wiki

     var MikroNode = require('mikronode');

     var device = new MikroNode('192.168.0.1');
     device.connect().then(([login])=>login('admin','password')).then(function(conn) {
        conn.closeOnDone(true); // when all channels are "done" the connection should close.

        var chan1=conn.openChannel("interface_listener");
        chan1.write('/interface/listen');
        chan1.data.subscribe(function(item) {
            var packet=MikroNode.resultsToObj(item.data);
            console.log('Interface change: '+JSON.stringify(packet));
        });

        // This should be called when the cancel is called below. (trap occurs first, then done)
        chan1.done.subscribe(function(packet) {
            // This should output everything that the above outputted.
            packet.data.forEach(function(data) {
                var packets=MikroNode.resultsToObj(data);
                console.log('Interface: '+JSON.stringify(packet));
            });
        });

        var chan2=conn.openChannel('config_interface');

        // added closeOnDone option to this call
        var chan3=conn.openChannel('enable_interface'); // We'll use this later.

        var chan4=conn.openChannel('getall_interfaces'); 

        chan2.write('/interface/set',{'disabled':'yes','.id':'ether1'});
        chan2.done.subscribe(function(items) {
            // We do this here, 'cause we want channel 4 to write after channel 3 is done.
            // No need to listen for channel3 to complete if we don't care.
            chan3.write('/interface/set',{'disabled':'no','.id':'ether1'});

            chan4.write('/interface/getall');

            // Alternative (legacy) way of caturing when chan4 is done.
            chan4.on('done',function(packet) {
                packet.data.forEach(function(data) {
                    var packets=MikroNode.resultsToObj(data);
                    console.log('Interface: '+JSON.stringify(packet));
                });
                chan1.close(); // This should call the /cancel command to stop the listen.
            });
        });
    });

Simplifying the above by reducing the number of channels.

Notice how the callback embedding is not needed using the syncronous capability.

    var MikroNode = require('mikronode');

    var device = new MikroNode('192.168.0.1');
     device.connect().then(([login])=>login('admin','password')).then(function(conn) {
        conn.closeOnDone(true); // All channels need to complete before the connection will close.
        var listenChannel=conn.openChannel();
        listenChannel.write('/interface/listen');

        // Each sentence that comes from the device goes through this.
        listenChannel.read.subscribe(function(data) {
            var packet=MikroNode.resultsToObj(data);
            console.log('Interface change: '+JSON.stringify(packet));
        });

        var actionChannel=conn.openChannel();
        actionChannel.sync(true);

        // These will run synchronsously
        actionChannel.write('/interface/set',{'disabled':'yes','.id':'ether1'});
        actionChannel.write('/interface/set',{'disabled':'no','.id':'ether1'});
        actionChannel.write('/interface/getall');
        actionChannel.on('done',function(packet) {
            packet.data.forEach(function(data) {
                var packets=MikroNode.resultsToObj(data);
                console.log('Interface: '+JSON.stringify(packet));
            });
            listenChannel.close(); // This should call the /cancel command to stop the listen.
        });
        actionChannel.close(); // The above commands will complete before this is closed.
    });

Promises add simplicity:

    var MikroNode = require('mikronode');
    var device = new MikroNode('192.168.0.1');
    device.connect().then(([login])=>login('admin','password')).then(function(conn) {
        console.log("Logged in.");
        conn.closeOnDone(true); // All channels need to complete before the connection will close.
        var listenChannel=conn.openChannel("listen");

        // Each sentence that comes from the device goes through the data stream.
        listenChannel.data.subscribe(function(data) {
            // var packet=MikroNode.resultsToObj(data);
            console.log('Interface change: ',JSON.stringify(data));
        },error=>{
            console.log("Error during listenChannel subscription",error) // This shouldn't be called.
        },()=>{
            console.log("Listen channel done.");
        });

        // Tell our listen channel to notify us of changes to interfaces.
        listenChannel.write('/interface/listen').then(result=>{
            console.log("Listen channel done promise.",result);
        })
        // Catch shuold be called when we call /cancel (or listenChannel.close())
        .catch(error=>console.log("Listen channel rejection:",error));

        // All our actions go through this.
        var actionChannel=conn.openChannel("action",false); // don't close on done... because we are running these using promises, the commands complete before each then is complete.

        // Do things async. This is to prove that promises work as expected along side streams.
        actionChannel.sync(false);
        actionChannel.closeOnDone(false); // Turn off closeOnDone because the timeouts set to allow the mikrotik to reflect the changes takes too long. The channel would close.

        // These will run synchronsously (even though sync is not set to true)
        console.log("Disabling interface");
        actionChannel.write('/interface/set',{'disabled':'yes','.id':'ether1'}).then(results=>{
            console.log("Disable complete.");
            // when the first item comes in from the listen channel, it should send the next command.
            const {promise,resolve,reject}=MikroNode.getUnwrappedPromise();
            listenChannel.data
                .take(1)
                // This is just to prove that it grabbed the first one.
                .do(d=>console.log("Data:",MikroNode.resultsToObj(d.data)))
                .subscribe(d=>actionChannel.write('/interface/set',{'disabled':'no','.id':'ether1'}).then(resolve,reject));
            return promise;
        })
        .then(results=>{
            console.log("Enabled complete.");
            // return new Promise((r,x)=>setTimeout(r,1000)).then(()=>actionChannel.write('/interface/getall'));
            const {promise,resolve,reject}=MikroNode.getUnwrappedPromise();
            // when the second item comes in from the listen channel, it should send the next command.
            listenChannel.data
                .take(1)
                // This is just to prove that it grabbed the second one.
                .do(d=>console.log("Data:",MikroNode.resultsToObj(d.data)))
                .subscribe(d=>actionChannel.write('/interface/getall').then(resolve,reject));
            return promise;
        })
        .then(results=>{
            var formatted=MikroNode.resultsToObj(results.data);
            var columns=[".id","name","mac-address","comment"];
            var filtered=formatted.map(line=>columns.reduce((p,c)=>{p[c]=line[c];return p},{}));
            console.log('Interface [ID,Name,MAC-Address]: ',JSON.stringify(filtered,true,4));
        })
        .catch(error=>{
            console.log("An error occurred during one of the above commands: ",error);
        })
        // This runs after all commands above, or if an error occurs.
        .then(nodata=>{
            console.log("Closing everything.");
            listenChannel.close(true); // This should call the /cancel command to stop the listen.
            actionChannel.close();
        });
    });

The methods decodeLength and encodeString were written based on code here on the Mikrotik Wiki.

License

(The MIT License)

Copyright (c) 2011,2012,2013,2014,2015,2016,2017 Brandon Myers trakkasure@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.