简体   繁体   English

如何在node.js中使用node-ssh断开ssh连接

[英]How to disconnect ssh connection by using node-ssh in node js

Can anyone help me, how t disconnect ssh connection using node-ssh module in node.js. Also how to handle error.谁能帮助我,如何在 node.js 中使用 node-ssh 模块断开 ssh 连接。还有如何处理错误。

my code is我的代码是

 driver = require('node-ssh');

    ssh = new driver({
              host: '192.168.*.*',
              username: 'user',
              password: 'password',
              privateKey : require('fs').readFileSync('/tmp/my_key')
            });

    ssh.connect().then(function() {
           /*
       some code
        */

            },function(error) {
                console.log(error);

    });

Pls help.请帮助。

Use the dispose method. 使用dispose方法。 Example: 例:

const node_ssh = require('node-ssh');
const ssh = new node_ssh();

ssh.connect({
  host: 'XXX',
  username: 'YYY',
  privateKey: 'ZZZ'
}).then(resp => {
  console.log(resp);
  ssh.dispose();
});

The 'node-ssh' wrapper doesn't seem to provide an 'end' function, or a way to access the underlying ssh2 connection object. 'node-ssh'包装器似乎不提供'end'函数,也不提供访问底层ssh2连接对象的方法。 That leaves you with a couple options: 这给你留下了几个选择:

Fork the source , write an 'end' method, and issue a pull request to the node-ssh wrapper so that future users can use the wrapper and end the connections. 分叉源代码 ,编写'end'方法,并向node-ssh包装器发出pull请求,以便将来的用户可以使用包装器并结束连接。 Alternatively, you can create an issue and wait for someone else to create the functionality if/when they deem it necessary. 或者,您可以创建问题并等待其他人在他们认为必要时创建功能。

Use the underlying ssh2 library instead. 请改用底层的ssh2库。 It exposes a lot more functionality to you, including an 'end' method, to close the connection, but uses callbacks instead of promises, which would require you to refactor your code. 它为您提供了更多功能,包括“结束”方法,以关闭连接,但使用回调而不是promises,这需要您重构代码。

Additionally, you could add the following to your code, though this is very heavily not recommended , as it's messing with a prototype you don't own and could ruin compatibility with future versions of the node-ssh module: 此外,您可以在代码中添加以下内容,但这是非常不推荐的 ,因为它正在弄乱您不拥有的原型并且可能破坏与node-ssh模块的未来版本的兼容性:

driver.prototype.end = function() {
  this.Connection.end()
  this.Connected = false
}

you can then call ssh.end() to close the connection. 然后,您可以调用ssh.end()来关闭连接。

I've asked directly the creator of the library, here i report the answer: 我直接询问了图书馆的创建者,在这里我报告了答案:

"Note: If anything of the ssh2 object is not implemented and I am taking more time than I should, You can always use MySSH.Connection.close() " “注意:如果没有实现ssh2对象的任何内容,并且我花费的时间比我应该的多,那么你总是可以使用MySSH.Connection.close()

Issue#9 问题#9

Then he has done a commit and now you have your method! 然后他做了一个提交,现在你有了方法!

ssh.end();

I struggle for these the ssh to a remote server frequently, and the connection can not be closed by ssh.compose() in the ssh-node package, and I find remote windows ssh server still generate many processes named sshd process and not be closed, and after ssh.compose , the node.js will throw the error Error: read ECONNRESET at TCP.onStreamRead which can not be caught by code.I find many references, it could be the TCP connection is still working on and not be closed.我经常将这些 ssh 连接到远程服务器,并且无法通过ssh.compose()在 ssh-node package 中关闭连接,我发现远程 windows ssh 服务器仍然生成许多名为sshd进程的进程并且未被关闭,并且在ssh.compose之后,node.js 将抛出错误Error: read ECONNRESET at TCP.onStreamRead which cannot be catched by code.I find many references, it could be the TCP connection is still working on and not be closed. So I try to refer to the ssh-node code and use the所以我尝试参考 ssh-node 代码并使用

ssh.connection.destroy()

which point to the Client.prototype.destroy method in ssh2 package, so the error Error: read ECONNRESET at TCP.onStreamRead will disappeared, and the connection will be closed totally.指向ssh2包中的Client.prototype.destroy方法,所以错误Error: read ECONNRESET at TCP.onStreamRead将消失,连接将完全关闭。

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

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