简体   繁体   English

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

[英]Node.js error: read ECONNRESET using ssh2

I am trying to use ssh2 from npm to setup an ssh connection and remotely login to a machine.我正在尝试使用 npm 中的 ssh2 来设置 ssh 连接并远程登录到一台机器。 The code works fine for Windows to Linux/MacOS connections but fails for Windows to Windows connection(the error screenshot has been attached).该代码适用于 Windows 到 Linux/MacOS 的连接,但无法用于 Windows 到 Windows 的连接(错误截图已附上)。 I did find something related to agent and agentForward parameters that might be causing the problem but couldn't find a solution to it.我确实找到了与 agent 和 agentForward 参数相关的东西,这些参数可能会导致问题,但找不到解决方案。 Node Version: 8.11.2节点版本:8.11.2
Any help would be appreciated!任何帮助,将不胜感激! Thanks!谢谢! The code is as follows:代码如下:

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);
});

This is the Error Message:这是错误消息:

在此处输入图片说明

I have also similar problem.我也有类似的问题。

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

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

Though I added some delay and it somehow works.虽然我添加了一些延迟并且它以某种方式起作用。

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

Add some delay or try-catch statement;添加一些延迟或 try-catch 语句; also with "stream.on('data', function(data) {...还有“stream.on('data', function(data) {...

stream.on('close', function(code, signal){
  console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
  //var newvar = oldvar;

  setTimeout(function() {
    conn.end();
    //use the newvar here..
    //extra lines of code..
  },100);
}

I had to give up on the above approach ie using ssh2 since I couldn't find a solution for the error being faced.我不得不放弃上述方法,即使用 ssh2,因为我找不到解决所面临错误的方法。

What I did instead was to run powershell scripts:我所做的是运行powershell脚本:

  • The first script simply ran the Test-Connection cmdlet to check whether the machine at a given IP address was reachable or not.第一个脚本只是运行Test-Connection cmdlet 来检查给定 IP 地址的机器是否可访问。
  • Next, a powershell script was run on the host machine using child-process in Node.js to get the required parameters(such as Hostname, RAM, OS etc.) from the remote machine which should be a Windows machine.接下来,使用 Node.js 中的子进程在主机上运行一个 powershell 脚本,以从应该是 Windows 机器的远程机器获取所需的参数(例如主机名、RAM、操作系统等)。
  • If the second script was not run on a windows machine, it would fail with an error and then a third powershell script would run which has been made specifically for Linux based machines.如果第二个脚本没有在 Windows 机器上运行,它将失败并显示错误,然后第三个 powershell 脚本将运行,该脚本专为基于 Linux 的机器制作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM