node.jssocketswebsocketrouterrouter-os

How to connect to RouterOS via Nodejs WebSocket?


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.


Solution

  • Take into consideration the next things:

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