How to connect to RouterOS via Nodejs WebSocket?

2.9k Views Asked by At

I'm learning websocket nodejs, I want to connect to routeros via websocket like the https://github.com/aluisiora/node-routeros/ package, the package is too broad, I just want to know how to connect.

I've read the official documentation https://wiki.mikrotik.com/wiki/Manual:API, but I'm having trouble understanding it.

I have tried it this way, but did not get any response:

client.connect(port, host, function () {
  console.log("Connected");
  client.write(encodeString("/login"));
  client.write(encodeString(`=name=${user}`));
  client.write(encodeString(`=password=${password}`));
});

client.on("data", function (data) {
  console.log("Received: " + data); // not excetue
});

I'm looking for code samples to connect to routeros via nodejs socket, hopefully someone shares here.

Thanks in advance, I really appreciate any answer.

1

There are 1 best solutions below

6
On BEST ANSWER

Take into consideration the next things:

  • RouterOS API has it's own protocol, it has a bit of complexity. The official wiki tell us how to interact with it at LOW LEVEL. For these reason it's very difficult to understand. Isn't for a High Level programmer. Don't worry, We have all been through here.
  • Routeros v7 have a REST API, that will make the job easier, the exchange language is HTTP protocol, easy right? Actually is at beta stage.
  • RouterOS Wiki have other package for node.js that seems more easy: Mikronode --> LINK REMOVED FROM WIKI TRY THIS PACKAGE INSTEAD

solution

Install mikronode package

 $ npm install mikronode

use it:

 var api = require('mikronode');

 var connection = new api('192.168.0.1','admin','password');
 connection.connect(function(conn) {

    var chan=conn.openChannel();

    chan.write('/ip/address/print',function() {
       chan.on('done',function(data) {

          var parsed = api.parseItems(data);

          parsed.forEach(function(item) {
             console.log('Interface/IP: '+item.interface+"/"+item.address);
          });

          chan.close();
          conn.close();

       });
    });
 });