How to disable ssh connection using node-sss in node js

Can someone help me how to disable ssh connection using node-sss module in node js. Also how to handle errors.

my code

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

Help Pls.

+5
source share
3 answers

The shell 'node -ssh' does not seem to provide an end function or a way to access the base ssh2 connection object. This leaves you with a couple of options:

Paste, write the end method and issue a request to migrate to the node -ssh shell so that future users can use the shell and terminate the connections. In addition, you can create an issue and wait for someone else to create this function if / when they consider it necessary.

Use the ssh2 core library instead . It provides you with many more features, including the end method, to close the connection, but uses callbacks instead of promises, which will require reorganization of your code.

In addition, you can add the following to your code, but it is very strongly not recommended , since it is messing with a prototype that you do not have, and can ruin compatibility with future versions of node -ssh:

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

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

+4
source

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(); }); 
+3
source

I asked the creator of the library directly, here I report the answer:

"Note. If any of the ssh2 object is not implemented, and I take more time than I should, you can always use MySSH.Connection.close() "

Problem number 9

Then he committed, and now you have your own method!

 ssh.end(); 
+2
source

All Articles