3

I'm using ssh2-sftp-client module and I'm getting this error

{"code":"ERR_GENERIC_CLIENT","custom":true}

I have no clue what I'm doing wrong and I haven't found any solution.

I tried accessing sftp server directly on the command line with the same config and I can connect just fine.

Here's the code

import Client from 'ssh2-sftp-client';
import config from 'config';
import logger from './logger';

const upload = async csvFiles => {
  try {
    console.log('masuk');
    const sftp = new Client();
    const SFTP = config.get('SFTP');
    const remote = SFTP.REMOTE;

    await sftp
      .connect({
        host: SFTP.HOST,
        port: SFTP.PORT,
        username: SFTP.USERNAME,
        password: SFTP.PASSWORD
      })
      .then(async () => {
        console.log('masuk upload');
        await Promise.all(
          csvFiles.map(async file => {
            sftp.put(file, remote);
          })
        );
      })
      .then(result => {
        logger.log({ level: 'info', message: 'sftp-success', result });
      });
  } catch (err) {
    logger.log({ level: 'error', message: 'sftp-error', err });
  } finally {
    console.log('keluar');
  }
};

module.exports = upload;

Update: I just found the full debug log

CLIENT[sftp]: connect: Debugging turned on
CLIENT[sftp]: connect: Connect attempt 1
CLIENT[sftp]: getConnection: Adding temp event listeners
CLIENT[sftp]: Adding listener to end event
CLIENT[sftp]: Adding listener to close event
CLIENT[sftp]: Adding listener to error event
CLIENT[sftp]: getConnection: created promise
CLIENT[sftp]: Adding listener to ready event
Custom crypto binding not available
Local ident: 'SSH-2.0-ssh2js1.11.0'
Client: Trying 10.49.128.143 on port 22 ...
CLIENT[sftp]: getConnection Error: Handling error: Timed out while waiting for handshake
CLIENT[sftp]: getConnection Error: handled error with reject
CLIENT[sftp]: Global: Ignoring handled error: Timed out while waiting for handshake
CLIENT[sftp]: getConnection: finally clause fired
Socket closed
CLIENT[sftp]: getConnection Close: handling unexpected close event
CLIENT[sftp]: getConnection Close: handling close event with reject
CLIENT[sftp]: Global: Ignoring handled close event
CLIENT[sftp]: Removing listener from ready event
CLIENT[sftp]: getConnection: Removing temp event listeners
CLIENT[sftp]: Removing listener from end event
CLIENT[sftp]: Removing listener from close event
CLIENT[sftp]: Removing listener from error event
CLIENT[sftp]: getConnection retry catch: getConnection: Timed out while waiting for handshake Code: ERR_GENERIC_CLIENT
CLIENT[sftp]: connect: Connect attempt 2
CLIENT[sftp]: getConnection: Adding temp event listeners
CLIENT[sftp]: Adding listener to end event
CLIENT[sftp]: Adding listener to close event
CLIENT[sftp]: Adding listener to error event
CLIENT[sftp]: getConnection: created promise
CLIENT[sftp]: Adding listener to ready event
Custom crypto binding not available
Local ident: 'SSH-2.0-ssh2js1.11.0'
Client: Trying 10.49.128.143 on port 22 ...
CLIENT[sftp]: getConnection Error: Handling error: Timed out while waiting for handshake
CLIENT[sftp]: getConnection Error: handled error with reject
CLIENT[sftp]: Global: Ignoring handled error: Timed out while waiting for handshake
CLIENT[sftp]: getConnection: finally clause fired
Socket closed
CLIENT[sftp]: getConnection Close: handling unexpected close event
CLIENT[sftp]: getConnection Close: handling close event with reject
CLIENT[sftp]: Global: Ignoring handled close event
CLIENT[sftp]: Removing listener from ready event
CLIENT[sftp]: getConnection: Removing temp event listeners
CLIENT[sftp]: Removing listener from end event
CLIENT[sftp]: Removing listener from close event
CLIENT[sftp]: Removing listener from error event
CLIENT[sftp]: getConnection retry catch: getConnection: Timed out while waiting for handshake Code: ERR_GENERIC_CLIENT
CLIENT[sftp]: connect: Error getConnection: Timed out while waiting for handshake
William Mandang
  • 103
  • 1
  • 11

1 Answers1

1

I solved the problem by changing my approach to the whole thing. as far as I know you shouldn't use await with promises like you did here:

await sftp
  .connect({
    host: SFTP.HOST,
    port: SFTP.PORT,
    username: SFTP.USERNAME,
    password: SFTP.PASSWORD
  })
  .then(async () => {...})

You should either use async/await or promises. Since connect is an async function, you can do it like this:

try {
    await sftp.connect(config);
    // your async work here
    // when you want to use put()
    let result = sftp.put(file, remote);
    console.log(result);
} catch (err) {
    console.log(err);
} finally {
    sftp.end();
}

I hope this helps in some way.