3

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It does work (the default timeout is 20 seconds) however it is unclear how to handle the timeout error. It just crashes the Node app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.