我的目标是制作一个允许其用户远程连接到 SSH 服务器的 Web 应用程序,即无需在他们的计算机上安装 SSH 客户端。所以这意味着我的服务器将成为用户和他们的 SSH 服务器之间的中间接口。
我找到了一个节点的 SSH2 模块:https ://github.com/mscdex/ssh2
我认为最合适的连接方法是使用该shell()
方法。
以下是让 shell 工作的基本尝试。
var Connection = require('ssh2');
var c = new Connection();
c.on('ready', function() {
c.shell(onShell);
});
var onShell = function(err, stream) {
if (err != null) {
console.log('error: ' + err);
}
stream.on('readable', function() {
var chunk;
while (null !== (chunk = stream.read())) {
console.log('got %d bytes of data', chunk.length);
}
});
stream.write('ls\r\n');
console.log('Shell');
}
c.connect({
host: 'localhost',
port: 22,
username: 'matt',
password: 'password'
});
它连接正常,没有错误,但没有显示“得到 %d 字节的数据”。我怎样才能解决这个问题?
此外,这种方法在可能同时存在许多不同连接的大规模应用程序中是否明智?