SSH2 クライアント ( https://www.npmjs.com/package/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: ''
}
ばかげていると確信していますが、何が悪いのか判断できないようです。