title: node typescript ssh2 connection disconnected on server with [preauth]
body: <p>Using the <code>ssh2</code> module, connecting to a server with a privateKey, I'm getting errors in the auth logs. The logs are shown below.</p>
  
  <p>The code for setting up the connection:</p>
  
  <pre class="lang-node prettyprint-override"><code>const Client = require('ssh2').Client;
  const fs = require('fs');
  const util = require('util');
  
  const key = fs.readFileSync('/path/to/rsa_key');
  
  const conn = new Client();
  await conn.connect({
      host: '&lt;REMOTE_IP&gt;',
      port: 22,
      username: '&lt;NAME&gt;',
      privateKey: key,
      // @ts-ignore
      debug: (d) =&gt; {
          console.log(util.inspect(d));
      },
  });
  
  await this.performTask(conn, 'uptime');
  
  async performTask(conn, command) {
      return new Promise((resolve, reject) =&gt; {
          // @ts-ignore
          conn.exec(command, (err, stream) =&gt; {
              if (typeof err !== 'undefined') {
                  // On error, stream is undefined.
                  console.error(`ERROR: ${err}`);
                  reject(new Error(err));
              } else {
                  // @ts-ignore
                  stream.on('close', (code, signal) =&gt; {
                      console.log(`Stream :: close :: code: ${code} , signal: ${signal}`);
                      conn.end();
                      resolve();
  
                      // @ts-ignore
                  }).on('data', (data) =&gt; {
                      console.log(`STDOUT: ${data}`);
                      // @ts-ignore
                  }).on('error', (errrrrr) =&gt; {
                      console.error(`B ${errrrrr}`);
                      // @ts-ignore
                  }).stderr.on('data', (data) =&gt; {
                      console.error(`STDERR: ${data}`);
                  });
              }
          });
      });
  }
  </code></pre>
  
  <pre><code>Sep  2 22:04:27 &lt;remote_host&gt; sshd[9037]: Disconnected from &lt;local_ip&gt; port 38194 [preauth]
  </code></pre>
  
  <p>Turning on <code>debug</code> prints different things. Either one of these 2:</p>
  
  <pre><code>'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
  'DEBUG: Client: Trying &lt;REMOTE_IP&gt; on port 22 ...'
  'DEBUG: Outgoing: Writing CHANNEL_OPEN (0, session)'
  'DEBUG: Client: Connected'
  'DEBUG: Parser: IN_INIT'
  'DEBUG: Parser: IN_GREETING'
  'DEBUG: Parser: IN_HEADER'
  'DEBUG: Remote ident: \'SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3\''
  'DEBUG: Outgoing: Writing KEXINIT'
  'DEBUG: Parser: IN_PACKETBEFORE (expecting 8)'
  'DEBUG: Parser: IN_PACKET'
  'DEBUG: Parser: pktLen:1076,padLen:6,remainLen:1072'
  'DEBUG: Parser: IN_PACKETDATA'
  'DEBUG: Parser: IN_PACKETDATAAFTER, packet: KEXINIT'
  'DEBUG: Comparing KEXINITs ...'
  &lt;alogirthm comparisons, no error message, just print of what is picked&gt;
  'DEBUG: Outgoing: Writing KEXECDH_INIT'
  'DEBUG: Parser: IN_PACKETBEFORE (expecting 8)'
  events.js:174
        throw er; // Unhandled 'error' event
        ^
  
  Error: write EPIPE
      at WriteWrap.afterWrite [as oncomplete] (net.js:788:14)
  Emitted 'error' event at:
      at Socket.&lt;anonymous&gt; (....../node_modules/ssh2/lib/client.js:307:10)
      at Socket.emit (events.js:203:15)
      at errorOrDestroy (internal/streams/destroy.js:107:12)
      at onwriteError (_stream_writable.js:436:5)
      at onwrite (_stream_writable.js:461:5)
      at _destroy (internal/streams/destroy.js:49:7)
      at Socket._destroy (net.js:613:3)
      at Socket.destroy (internal/streams/destroy.js:37:8)
      at WriteWrap.afterWrite [as oncomplete] (net.js:790:10)
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
  'DEBUG: Client: Trying&lt;REMOTE_IP&gt; on port 22 ...'
  'DEBUG: Outgoing: Writing CHANNEL_OPEN (0, session)'
  'DEBUG: Client: Connected'
  'DEBUG: Parser: IN_INIT'
  'DEBUG: Parser: IN_GREETING'
  'DEBUG: Parser: IN_HEADER'
  'DEBUG: Remote ident: \'SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3\''
  'DEBUG: Outgoing: Writing KEXINIT'
  ERROR: Error: No response from server
  </code></pre>
  
  <p>Either of those happens without a predictable pattern.</p>