简体   繁体   English

如何使用节点ssh2启动用户交互式ssh会话?

[英]How can I use node ssh2 to start a user interactive ssh session?

I've been trying to find a way to make a node script automatically open up an ssh connection for me. 我一直在尝试找到一种使节点脚本自动为我打开ssh连接的方法。 I don't want it to run any command, just open the connection and let me start typing. 我不希望它运行任何命令,只需打开连接并让我开始输入即可。

I've found this question on ways to connect: SSH client for Node.js 我在连接方式上发现了这个问题: Node.js的SSH客户端

Every way I've found thus far has been primarily focused on opening an ssh connection and running commands with node, but I don't want any commands to be run but the ones I type myself. 到目前为止,我发现的每一种方式都主要集中在打开ssh连接和使用node运行命令,但是我不希望运行任何命令,而是我自己输入的命令。

The package I'm trying to use is ssh2 https://github.com/mscdex/ssh2#installation 我要使用的软件包是ssh2 https://github.com/mscdex/ssh2#installation

It connects well, but I can't find an example of a way to connect process.stdio to it easily. 它可以很好地连接,但是我找不到用于将process.stdio轻松连接到它的方法的示例。 I can imagine convoluted ways of doing it, but it seems like there must be some very simple way. 我可以想象这样做的方法很复杂,但是似乎必须有一些非常简单的方法。

I have read the section on "interactive shell session" but it appears to be a bit of a misnomer as it really just runs ls -l then exits. 我已经阅读了有关“交互式shell会话”的部分,但似乎有点用词不当,因为它实际上只是运行ls -l然后退出。 No interaction there at all. 根本没有任何互动。

https://github.com/mscdex/ssh2#start-an-interactive-shell-session https://github.com/mscdex/ssh2#start-an-interactive-shell-session

What is the proper way to use this tool to start a normal, basic, plain ol' tty ssh session? 使用此工具启动正常的,基本的,普通的tty ssh会话的正确方法是什么?

I ended up solving the issue. 我最终解决了这个问题。 It's very strange that any issue arose as (when checking the source for ssh2) it appears that is set up to do exactly what I thought it should. 奇怪的是,出现了任何问题(在检查ssh2的源代码时)似乎设置为完全按照我的想法执行。 In any case, this code worked for me: 无论如何,这段代码对我有用:

const {host, password, port, username} = sshCreds;
return new Promise((resolve) => {
    var conn = new Client();
    conn.on('ready', function() {
        console.log('Client :: ready');
        conn.shell(function(err, stream) {
            if (err) throw err;

            const stdinListener = (data) => {
                skipNext = true;
                stream.stdin.write(data);
            };

            stream.on('close', function() {
                process.stdin.removeListener("data", stdinListener)
                conn.end();
                resolve();
            }).stderr.on('data', function(data) {
                resolve();
            });

            // skip next stops double printing of input
            let skipNext = false;
            stream.stdout.on("data", (data) => {
                if (skipNext) { return skipNext = false; }
                process.stdout.write(data);
            })

            process.stdin.on("data", stdinListener)
        });
    }).connect({
        host,
        port,
        username,
        password,
    });
})

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

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