使用 [preauth] 在服务器上断开节点打字稿 ssh2 连接

node typescript ssh2 connection disconnected on server with [preauth]

使用 ssh2 模块,使用私钥连接到服务器,我在授权日志中收到错误。日志如下所示。

建立连接的代码:

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: '<REMOTE_IP>',
    port: 22,
    username: '<NAME>',
    privateKey: key,
    // @ts-ignore
    debug: (d) => {
        console.log(util.inspect(d));
    },
});

await this.performTask(conn, 'uptime');

async performTask(conn, command) {
    return new Promise((resolve, reject) => {
        // @ts-ignore
        conn.exec(command, (err, stream) => {
            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) => {
                    console.log(`Stream :: close :: code: ${code} , signal: ${signal}`);
                    conn.end();
                    resolve();

                    // @ts-ignore
                }).on('data', (data) => {
                    console.log(`STDOUT: ${data}`);
                    // @ts-ignore
                }).on('error', (errrrrr) => {
                    console.error(`B ${errrrrr}`);
                    // @ts-ignore
                }).stderr.on('data', (data) => {
                    console.error(`STDERR: ${data}`);
                });
            }
        });
    });
}
Sep  2 22:04:27 <remote_host> sshd[9037]: Disconnected from <local_ip> port 38194 [preauth]

开启debug打印不同的东西。以下两个之一:

'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
'DEBUG: Client: Trying <REMOTE_IP> 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 ...'
<alogirthm comparisons, no error message, just print of what is picked>
'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.<anonymous> (....../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)

或:

'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
'DEBUG: Client: Trying<REMOTE_IP> 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

这两种情况都没有可预测的模式。

现在您必须等待 'ready' 事件才能发出命令,因为在连接完全准备好之前当前没有使用 request/command 队列。