5

Can anyone help me, how t disconnect ssh connection using node-ssh module in node js. Also how to handle error.

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.

selvan
  • 1,183
  • 3
  • 16
  • 24

4 Answers4

13

Use the dispose method. 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();
});
bersling
  • 17,851
  • 9
  • 60
  • 74
  • 1
    @AmauryD Accepted answers are a joke anyways, they almost never change and are often not the best solution... example: https://stackoverflow.com/questions/16267339/s3-static-website-hosting-route-all-paths-to-index-html – bersling Apr 27 '18 at 09:04
4

The 'node-ssh' wrapper doesn't seem to provide an 'end' function, or a way to access the underlying ssh2 connection object. 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. 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. 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.

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:

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

you can then call ssh.end() to close the connection.

Cody Haines
  • 1,157
  • 10
  • 16
2

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()"

Issue#9

Then he has done a commit and now you have your method!

ssh.end();
SharpEdge
  • 1,782
  • 1
  • 10
  • 20
0

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. So I try to refer to the ssh-node code and use the

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.

Brance Lee
  • 131
  • 1
  • 4