简体   繁体   English

在 shell 中使用 Node 模块 SSH2

[英]Using the Node module SSH2 with shell

My aim is to make a web application that allows it's user to connect to an SSH server remotely, ie without installing an SSH client on their computer.我的目标是制作一个 Web 应用程序,允许它的用户远程连接到 SSH 服务器,即无需在他们的计算机上安装 SSH 客户端。 So this means my server will be an intermediate interface between the user and their SSH server/s.所以这意味着我的服务器将成为用户和他们的 SSH 服务器之间的中间接口。

I found an SSH2 module for node: https://github.com/mscdex/ssh2我为节点找到了一个 SSH2 模块: https : //github.com/mscdex/ssh2

I figured the most appropriate method of connecting was using the shell() method.我认为最合适的连接方法是使用shell()方法。

The following is a basic attempt to get a shell working.以下是让 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'
});

It connects fine, and there are no errors, but the "got %d bytes of data" does not show.它连接正常,并且没有错误,但是“得到 %d 个字节的数据”没有显示。 How can I fix this?我该如何解决这个问题?

Furthermore would this method be sensible in a large scale application with potentially many simultaneous different connections?此外,这种方法在可能同时存在许多不同连接的大规模应用程序中是否合理?

ssh2 currently does not use streams2 yet because things get tricky because of the two (stdout and stderr) "sub-"streams and trying to handle backpressure in a sane way. ssh2 目前还没有使用streams2,因为事情变得棘手,因为两个(stdout 和stderr)“子”流并试图以理智的方式处理背压。 I am still hoping I can find some solution via streams2 though.我仍然希望我可以通过流 2 找到一些解决方案。

For now, you will have to listen for 'data', 'drain', etc. events instead.现在,您将不得不监听 'data'、'drain' 等事件。

UPDATE: ssh2 v0.3.x now supports streams2.更新: ssh2 v0.3.x 现在支持streams2。

I wrote a wrapper class for ssh2 that handles running multiple commands in a SSH shell session and can SSH tunnel to multiple hosts using nested host objects.我为 ssh2 编写了一个包装类,它处理在 SSH shell 会话中运行多个命令,并且可以使用嵌套的主机对象通过 SSH 隧道连接到多个主机。 Each hosts config object has its own commands, command event handlers, end of session handler and authentication parameters.每个主机配置对象都有自己的命令、命令事件处理程序、会话结束处理程序和身份验证参数。 Commands responses can be checked for prompts or results that require extra handling or addition/removal of commands from a commands array.可以检查命令响应是否有需要额外处理或从命令数组中添加/删除命令的提示或结果。 It also times out if a command produces an unexpected prompt that is not handled and would normally cause the process to hang waiting for a response it will never get.如果命令产生未处理的意外提示并且通常会导致进程挂起等待永远不会得到的响应,它也会超时。

Check it out:检查一下:

Extensive info in the readme, test examples provided, additional info in the git wiki.自述文件中的大量信息,提供的测试示例,git wiki 中的其他信息。 https://www.npmjs.org/package/ssh2shell https://www.npmjs.org/package/ssh2shell

https://github.com/cmp-202/ssh2shell https://github.com/cmp-202/ssh2shell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM