Skip to content

Catch error thrown after connection is rejected - Question #979

@JRobertsDS

Description

@JRobertsDS

I have the following code to capture the fingerprint of a server for (server) testing.
I reject the connection after capturing the fingerprint by returning "false" from the hostVerifier.
Doing so causes an error (KEY_EXCHANGE_FAILED) that I would like to catch so that it doesn't propagate -- it's expected behavior in my testing environment.
But I am baffled as to how to catch this error, and am looking for ideas.

var Connection = require('ssh2');

function getSSHFingerprintSSH2(ip, port) {
  return new Promise(resolve => {
    let fingerprintToReturn = "fingerprint error";
    let sshConnection = new Connection();

    sshConnection.on('error', function() {
      //normal operation resolves here after rejecting the connection
      resolve(fingerprintToReturn);
    });
    sshConnection.connect({
      host: ip,
      port: port,
      username: '',
      password: '',
      hostHash: 'sha256',
      hostVerifier: function(fingerprint) {
        fingerprintToReturn = fingerprint;
        // returning "false" rejects the connection with a handshake error
        // which is what we want to do normally, having captured the fingerprint
        return false;
      },
    });
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions