sanketbajoria / ssh2-promise

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

No response from server #78

Open flyfishMT opened 2 years ago

flyfishMT commented 2 years ago

I consistently get the error

Error: No response from server at Socket. (/app/node_modules/ssh2-promise/node_modules/ssh2/lib/client.js:764:19) at Socket.emit (events.js:315:20) at TCP. (net.js:673:12)

The first few times I call a function that uses your library. Eventually it works. I know that this is an error from ssh2, but thought that it might be cause be the usage of ssh2 in ssh2-promise, or how I'm using ssh2-promise?

Psuedo-code:

const ssh = new SSH2Promise({
  host: <hostname>,
  username: <username>,
  identity: <path to private key>,
});

module.exports = async (searchText) => {
  const MAX = 100;
  const fileToSearch = <path to file>;
  const cmd = `/bin/grep -wi ${searchText} ${fileToSearch} | tail -n ${MAX} | sort -r`;
  const stdout = await ssh.exec(cmd);
  const results = stdout && stdout.split('\n').filter((i) => i !== '');
  return results;
};

I appreciate any help pointing me in the right direction.

sanketbajoria commented 2 years ago

@flyfishMT can you please enable debug log, by passing debug function to SSH configuration. Most probably ssh server is getting disconnected.

flyfishMT commented 2 years ago

@sanketbajoria thanks I will work on this and report back. It's a little difficult as we are only encountering this error in production

flyfishMT commented 2 years ago

@sanketbajoria I was able to reproduce but don't want to post everything up, any advice on what I'm looking for? The first error I see is on connect:

Outbound: Sending CHANNEL_OPEN (r:13, session)
Socket error: read ECONNRESET
sanketbajoria commented 2 years ago

@flyfishMT Can you please explain.. how are you using this library Do we open single ssh connection. And, then, we use this same ssh connection to exec command. (Is there a lot of time gap, in between to exec command.) Or do we open new connection every time, to exec the command.

flyfishMT commented 2 years ago

@sanketbajoria the code in my original post is basically exactly how I'm using, that is a "helper" module, and is used by an express endpoint. So the the SSH2Promise object is declare at the module level, and used in the the exported function.

It looked to me like your exec function handles the ssh connection for me, I don't explicitly connect anywhere.

const helper = require('../helper');

router.get('/:searchText', async (req, res, next) => {
   // ...
   const results = await helper(searchText);
   // ...
};
sanketbajoria commented 2 years ago

@flyfishMT Seems to me ssh connection is dropping. Can you set KeepAliveInterval, so that persistent connection can be established.

const ssh = new SSH2Promise({
  host: <hostname>,
  username: <username>,
  identity: <path to private key>,
  keepaliveInterval: 60000,
 keepaliveCountMax: 5
});

Or for every request. you can create new ssh connection... exec... and close... But, if the number of request are high... go with above method In the meanwhile i will try to debug if connection get dropped, then it should get established automatically.

flyfishMT commented 2 years ago

Hi @sanketbajoria any luck debugging? I thought I had success with with keepaliveCountMax but it cropped up again. However, I don't remember keepaliveInterval being suggested so I haven't tried that. I also tried to just connect and close every time but am now seeing "Error: No Connection" for a time, and then it will work intermittently. For option 2 of re-creating the connection every time, do you recommend instantiating at the module level as in my first comment, or perhaps re-instantiate every time in the exported function that does the work?