node.js connection error ECONNRESET on remote wind

2019-08-21 10:33发布

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条回答
时光不老,我们不散
2楼-- · 2019-08-21 11:15

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. :-)

查看更多
登录 后发表回答