node.js - NODEJS/SSH2 - 将 SSH 客户端重新连接到服务器失败:ADMINISTRATIVELY_PROHIBITED
问题描述
尝试创建 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: ''
}
我确信这是愚蠢的,但我似乎无法确定什么是错的。
解决方案
尝试将 SSH 客户端的实例化移动到原型方法prototype.SSHConnect
函数。ssh2.client.end
关闭套接字。
//setup connection
clientSSH.prototype.SSHConnect = function(){
this.SSHConn = new ssh.Client();
//setup listeners
this.SSHConn.on('ready', ()=>{
推荐阅读
- python - TypeError:不支持的操作数类型/:'float'和'csr_matrix'
- c# - Azure 服务总线“ReceiveAsync”
- java - 在hibernate中,JSP值如何匹配Entity类中的变量
- python - 无法删除包含零的 pandas DataFrame 中的行
- cherrypy - python3的Debian软件包安装
- hdfs - 如何在java代码中访问HDFS扩展属性
- python - 如何将新的支付网关与 django-payments 集成?
- vb.net - 调用带有客户端证书错误的 SOAP Web 服务 - 从服务器接收的身份验证标头是“”
- javascript - 动态创建“ul”和“li”并配置“单选按钮”的错误消息
- hadoop - 如何将influxDB连接到hadoop以将influxdb数据存储到hdfs中?