Node.js错误:使用ssh2读取ECONNRESET

时间:2018-06-22 18:45:46

标签: javascript node.js npm

我正在尝试使用npm中的ssh2来建立ssh连接并远程登录到计算机。该代码对于Windows到Linux / MacOS的连接正常,但是对于Windows到Windows的连接则无效(错误屏幕截图已附加)。我确实找到了与agent和agentForward参数相关的内容,可能会导致问题,但找不到解决方案。节点版本:8.11.2
任何帮助,将不胜感激!谢谢! 代码如下:

app.get("/",function(req,res)
{
    var conn = new Client();
    conn.on('ready', function() 
    {
        console.log('Client :: ready');
        conn.exec('systeminfo', function(err, stream) 
        {//system_profiler SPHardwareDataType, systeminfo
            if (err) console.log(err);
            stream.on('close', function(code, signal) 
            {
            console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
            conn.end();
            }).on('data', function(data) 
            {
                console.log('STDOUT: ' + data);
            }).stderr.on('data', function(data) 
            {
                console.log('STDERR: ' + data);
            });
        });
    }).connect({
    host: '127.0.0.1',
    port: 22,
    username: 'inspiron',
    privateKey: require('fs').readFileSync('C:/Users/inspiron/Downloads/private_key.ppk')
    });
    res.send(str);
});

这是错误消息:

enter image description here

2 个答案:

答案 0 :(得分:0)

我也有类似的问题。

library(dplyr)
library(tidyr)

dt %>%
  gather(key, value, -Date, na.rm = TRUE) %>%
  count(Date, value) %>%
  spread(value, n, fill = 0)

#   Date   `5`   `6`   `7`
#  <dbl> <dbl> <dbl> <dbl>
#1     1     2     1     0
#2     2     0     2     2

尽管我增加了一些延迟,但它以某种方式起作用。

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

Error: read ECONNRESET
    at _errnoException (util.js:992:11)
    at TCP.onread (net.js:618:25)

添加一些延迟或try-catch语句;也与“ stream.on('data',function(data){...

stream.on('close', function(code, signal){
  console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
  //extra lines of code here..
  conn.end();
}

答案 1 :(得分:0)

我不得不放弃上述方法,即使用ssh2,因为我找不到面对错误的解决方案。

我要做的是运行Powershell脚本:

  • 第一个脚本仅运行Test-Connection cmdlet来检查给定IP地址上的计算机是否可访问。
  • 接下来,使用Node.js中的child-process在主机上运行Powershell脚本,以从应该是Windows机器的远程机器上获取所需的参数(例如主机名,RAM,操作系统等)。 。
  • 如果第二个脚本未在Windows计算机上运行,​​则会失败并显示错误,然后将运行专门针对基于Linux的计算机制作的第三个powershell脚本。