1

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.

R.M. Reza
  • 725
  • 1
  • 8
  • 20

1 Answers1

1

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();

       });
    });
 });
gilito
  • 354
  • 3
  • 11
  • So far I have used routeros node package, everything works fine. However, because the development was stopped, I wanted to make my own package. Now that I know how it works and that I can connect to routeros using my own code after looking at some of the packages on Github, I have to learn about data buffers. Regarding RouterOS V7 REST API, this is a cool feature, I can't wait to try it in the stable version, when will this feature be released in a stable? – R.M. Reza Mar 09 '21 at 01:57
  • I think nobody knows ;) v7 is in beta state for more than one year...or two ;) If you understant the low level procedures, go ahead! Please, if you consider that the response is useful, check it ;) Many thanks! – gilito Mar 09 '21 at 15:28
  • Hello from future. Sadly that currently this package is no longer work. It can't resolve host name and if I use IP address, it will be always incorrect username or password. (RouterOS 6.49) – vee Apr 24 '23 at 06:31
  • 1
    Update: [This repository](https://github.com/aluisiora/node-routeros) seems to be working fine even it's discontinued. – vee Apr 24 '23 at 06:41
  • 1
    Thanks @vee I will update the answer with your information. Although I recommend to use HTTP REST API, is more standard, serialization with JSON... is the future... or... is the present! – gilito May 05 '23 at 17:06
  • REST API is for v7+ but I'm currently using latest of v6 and can't upgrade because a lot of WiFi users will be lost. – vee May 05 '23 at 17:17