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