Reputation: 1245
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.
Upvotes: 1
Views: 2795
Reputation: 11
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.
Upvotes: 1