【问题标题】:node typescript ssh2 connection disconnected on server with [preauth]使用 [preauth] 在服务器上断开节点 typescript ssh2 连接
【发布时间】:2019-09-04 22:02:39
【问题描述】:

使用 ssh2 模块,使用 privateKey 连接到服务器,我在身份验证日志中遇到错误。日志如下所示。

建立连接的代码:

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

其中任何一种情况都没有可预测的模式。

【问题讨论】:

  • 您是否尝试在调用conn.exec() 之前等待conn 上的'ready' 事件?
  • 嗯。似乎我在某个时候删除了它......然后似乎完全忘记了事件在节点中是如何工作的。谢谢,我试试看。
  • @mscdex 是的,就是这样。如果你愿意,你可以完全回答这个问题,我会接受。您的选择。

标签: node.js typescript ssh sshd


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2022-08-03
    • 2016-02-24
    • 2022-09-28
    • 2020-07-08
    相关资源
    最近更新 更多