Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node js set timeout in ssh in ssh2

Tags:

node.js

ssh

How can I set a timeout for the connection to a ssh server?

Currently, I am connecting to the server with the following code:

var Client = require('ssh2');

var conn = new Client();
conn.on('ready', function(){
    console.log("connected");
});
conn.on('error', function(){
    console.log("fails");
});
conn.connect({
    host: ip,
    port: 22,
    username: user,
    password: password
});
like image 237
beginerdeveloper Avatar asked Nov 20 '25 08:11

beginerdeveloper


1 Answers

You can set readyTimeout in the connect options like this :

conn.connect({
    host: ip,
    port: 22,
    username: user,
    password: password,
    readyTimeout: 5000
});

This will cancel any connection that is not ready within 5 seconds.

You can read more about it in the documentation of ssh2.

like image 137
drinchev Avatar answered Nov 23 '25 03:11

drinchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!