1

In a node.js project, I have installed and configured OPENSSH on a remote windows machine and trying to execute commands from local windows server using ssh2 (latest version) package. There is one exe file on remote machine which keeps on running until use cancel the execution.

Here is my route file code to serve user request.

const express = require('express');
const Route = express.Router();
var Client = require('ssh2').Client;
var conn = new Client();

Route.route('/start').get(function(req, res) {

    var v_host = 'xxx.xxx.xxx.xx';
    var v_user = 'Administrator';
    var v_pass = 'password';

    conn.on('ready', function() {
        console.log('Client :: start-ready');
        conn.exec('C:\\Work\\echousers.exe', function(err, stream) {
            if (err) {
                console.log('FIRST :: forwardOut error: ' + err);
                return conn.end();
            }
            stream.on('close', function(code, signal) {
                console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
                conn.end();
            }).on('data', function(data) {
                console.log('connname :' + data);
            });
        });
    }).connect({
        host: v_host,
        port: 22,
        username: v_user,
        password: v_pass
    });

    res.end();
});

Route.route('/stop').get(function(req, res) {
    //conn.destroy();
    conn.end(); 

    res.end();
});

module.exports = Route; 

It works on start request and "start" the batch execution but when i send "stop" request it throws error and crashes the node server.

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27) 
Emitted 'error' event at:
    at Socket.<anonymous> (D:\work\angular\app\server\node_modules\ssh2\lib\client.js:307:10)
    at Socket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)    
    at process._tickCallback (internal/process/next_tick.js:63:19)
[nodemon] app crashed - waiting for file changes before starting...

Please help me to handle this error. Struggling for many days to resolve this error. Many thanks in advance.

usersam
  • 1,125
  • 4
  • 27
  • 54
  • Start simpler. Get rid of that express code first: does the code _inside_ your middleware function even work on its own? – Mike 'Pomax' Kamermans Feb 08 '20 at 14:08
  • The stop route does not work on own because the conn variable is define which is supposed to end in stop route. – usersam Feb 08 '20 at 14:16
  • Have you tried stream.on("error", ...)? – JeffRSon Feb 08 '20 at 14:19
  • @JeffRSon, i tried for stream, .on('error', function(err) { console.log('err', err); }) but same error and node server crashes. – usersam Feb 08 '20 at 14:43
  • `conn` has not stop "route", it has a stop function, and since `conn` is defined through ssh2, not express, you can remove all the express code. So I guess the real question now is: why are you calling an exe file rather than doing what that does in Node itself? And if you absolutely must, use `spawn` rather than `exec` so you can better handle stdout/stderr from it. – Mike 'Pomax' Kamermans Feb 08 '20 at 14:48
  • And what about conn.on('error', ...)? – JeffRSon Feb 08 '20 at 18:01

1 Answers1

1

You need to add error handling:

conn.on('error', function(err) {
        //Handle Error here
})

I had the same issue. The SSH2 error handling does not go through standard event emission.

  • This should be the accepted answer, I couldn't find anything, not even a closed issue on ssh2's github regarding that and all other threads concerning this behavior have the same kind of asnwers (ie: "use something else"). Thanks for your input on that. One could argue that error handling should be implemented no matter what, it's bad practice otherwise. In development stage however, this is no the first thing i did or even thought about when building the actual server. – N.K Jan 20 '23 at 15:12