node.js connection error ECONNRESET on remote windows ssh

二次信任 提交于 2019-12-25 01:17:16

问题


I installed openssh on my remote windows server and tested it with putty.Now I want to connect to remote server through node.js , hence installed one package "simple-ssh". This package is also working fine with remote linux server but throws error with windows server.

here is my code file,

var SSH = require('simple-ssh');
var ssh;
ssh = new SSH({
            host: 'XXX.XXX.XX.101',
            user: 'username',
            pass: 'password'
        });

var command = 'typeperf "\\Processor(_Total)\\% Processor Time"'

console.log('command :'+command)

ssh.exec(command, {
    out: function(stdout) {
        console.log(stdout);
    }
}).start();

ssh.on('error', function(err) {
            console.log('something went wrong.');
            console.log(err);           
            ssh.end();
        });

And below error i get in console,

command :typeperf "\Processor(_Total)\% Processor Time"
something went wrong.
{ Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read',
  level: 'client-socket' }

please note, it works when i execute with putty session or when i put var command = 'ping -t XXX.XXX.XX.101' , but not with var command = 'typeperf "\\Processor(_Total)\\% Processor Time"' through node.js script. please help to resolve this issue as i have been struggling for weeks.

Many thanks in advance.


回答1:


I have solved the problem myself. After putting lots of effort and hit & trials, i came to know that it was mainly because of handling double quotations in input string. I handled this by putting escape characters before double quotations and problem got resolved.

I changed

var command = 'typeperf "\\Processor(_Total)\\% Processor Time"'

to

var command = 'typeperf \\"\\Processor(_Total)\\% Processor Time\\"'

And it worked. :-)



来源:https://stackoverflow.com/questions/55669800/node-js-connection-error-econnreset-on-remote-windows-ssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!