Node.js NODEJS/SSH2-将SSH客户端重新连接到服务器失败:管理禁止

Node.js NODEJS/SSH2-将SSH客户端重新连接到服务器失败:管理禁止,node.js,ssh2,Node.js,Ssh2,尝试创建SSH2客户端的通用抽象,在断开连接并尝试重新连接到SSH服务器时遇到问题。目前的代码如下 var ssh = require('ssh2'); function clientSSH(addr, port, type, user, pass){ this.addr = addr; this.port = port; this.type = type; this.user = user; this.pass = pass; this.SS

尝试创建SSH2客户端的通用抽象,在断开连接并尝试重新连接到SSH服务器时遇到问题。目前的代码如下

var ssh = require('ssh2');

function clientSSH(addr, port, type, user, pass){
    this.addr = addr;
    this.port = port;
    this.type = type;
    this.user = user;
    this.pass = pass;

    this.SSHConn = new ssh.Client();
    this.SSHConn.connected = false;
    this.SSHStream = null;
}

//setup connection
clientSSH.prototype.SSHConnect = function(){
    //setup listeners
    this.SSHConn.on('ready', ()=>{
        this.SSHConn.shell( (err, stream)=>{
            if (err) throw err;

            stream.on('close', ()=>{
                this.report('SSH connection with ' + this.addr + ' on port ' + this.port + ' was closed');
            })
            .on('data', (data)=>{
                this.report(data.toString());
            });
            this.SSHStream = stream;
        });
    });

    this.SSHConn.on('keyboard-interactive', (name, instructions, instructionsLang, prompts, finish)=>{
        this.report('Using Interactive Keyboard');
        finish([this.pass]);
    });

    this.SSHConn.on('error', (err)=>{
        this.report('Error with SSH connection: ' + err);
    });

    //check connection state before proceeding
    if(this.SSHConn.connected === false){
        this.SSHConn.connect({ host: this.addr, port: this.port, username: this.user, password: this.pass, tryKeyboard: true});
        this.SSHConn.connected = true;
        this.report('SSH connection established with ' + this.addr + ' on port ' + this.port);
    }
    else{
        this.report('SSH connection already established with ' + this.addr + ' on port ' + this.port);
    }
}

clientSSH.prototype.SSHDisconnect = function(){
    if(this.SSHConn.connected === true){
        this.SSHConn.end();
        this.SSHConn.connected = false;

        this.report('SSH connection with ' + this.addr + ' on port ' + this.port + ' was closed');
    }
    else{
        this.report('SSH connection already closed with ' + this.addr + ' on port ' + this.port);
    }
}

clientSSH.prototype.SSHWrite = function(command){
    try{
        this.SSHStream.write(command + '\r\n');
    }
    catch(err){
        this.report(err.code);
    }
}

module.exports = clientSSH;
到目前为止,除了重新连接,一切都正常。当我先断开连接,然后再连接时,我得到以下错误

Error: (SSH) Channel open failure:
    at SSH2Stream.onFailure (C:\Users\XXXX\node_modules\ssh2\lib\client.js:1205:13)
    at Object.onceWrapper (events.js:309:26)
    at SSH2Stream.emit (events.js:219:5)
    at parsePacket (C:\Users\XXXX\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:3552:10)
    at SSH2Stream._transform (C:\Users\XXXX\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:694:13)
    at SSH2Stream.Transform._read (_stream_transform.js:191:10)
    at SSH2Stream._read (C:\Users\XXXX\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:253:15)
    at SSH2Stream.Transform._write (_stream_transform.js:179:12)
    at doWrite (_stream_writable.js:461:12)
    at writeOrBuffer (_stream_writable.js:443:5) {
  reason: 'ADMINISTRATIVELY_PROHIBITED',
  lang: ''
}

我确信这是件愚蠢的事情,但我似乎无法确定是什么错。

尝试将SSH客户端的实例化移动到prototype方法prototype.SSHConnect函数。ssh2.client.end关闭套接字

//setup connection
clientSSH.prototype.SSHConnect = function(){
    this.SSHConn = new ssh.Client();
    //setup listeners
    this.SSHConn.on('ready', ()=>{