sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 25 forks source link

readyTimeout not honored on connection attempt #47

Closed ReevesL closed 4 years ago

ReevesL commented 4 years ago

Hi all,

I was attempting to shorten the time to failure for unresponsive machines but found the readyTImeout doesn't behave as expected. With SSH2 if I set readyTimeout to 2000 and attempt to connect to a non-existent machine it fails in 2 seconds as expected. With ssh2-promise the failure takes over a minute.

Here is sample code to demonstrate what I'm seeing. There are code blocks for ssh2-promise and ssh2. I would expect the behavior to be identical.

ssh2-promise: long time for connection failure

var SSH2Promise = require("ssh2-promise");

var theIp = '192.168.1.242';
var theUser = 'root'
var readyTimeout = 2000;

var sshconfig = {
  host: theIp,
  username: theUser,
  readyTimeout: readyTimeout,
};

var ssh = new SSH2Promise(sshconfig);

//Promise
ssh.connect().then(() => {
  console.log("Success");
  ssh.end();
}, () => {
  console.log("Failure"); 
  ssh.end();
});

ssh2: fails in 2sec as expected

var Client = require('ssh2').Client;

var theIp = '192.168.1.242';
var theUser = 'root'
var readyTimeout = 2000;

var conn = new Client();

conn.on('ready', function() {
  console.log('Success');
  conn.end();
})
.on('error', function(err) {
  console.log('Failure');
  conn.end();
})
.connect({
  host: theIp,
  username: theUser,
  readyTimeout: readyTimeout,
});
sanketbajoria commented 4 years ago

@ReevesL Can you please try with reconnect false, ssh2-promise have additional configuration for reconnect option

var sshconfig = {
  host: theIp,
  username: theUser,
  readyTimeout: readyTimeout,
 reconnect: false
};
ReevesL commented 4 years ago

reconnect: false

That did the trick, thanks!