【问题标题】:How do you create a terminal instance within a NodeJS child process?如何在 NodeJS 子进程中创建终端实例?
【发布时间】:2019-06-08 16:27:28
【问题描述】:

我正在设置一个不和谐通道以用作 SSH 终端。 NodeJS 服务器将提供连接。自定义命令将生成一个新的终端实例,然后可以将其用作 shell。

我不知道如何在子进程中生成终端。我尝试使用 screen 和 bash 命令无济于事。

我使用的是 CentOS 7。

// Code For Discord
var $discord = {
    currentInterface: null,
    send: (data) => {
        /* some code that sends data to a discord channel */
    },
    receive: (data) => {

        // Send Data To Terminal
        if ($discord.currentInterface) {
            $discord.currentInterface.send(data);
        } else {
            $discord.send('**Error:** Terminal has not been spawned.');
        }
    },
    command: (name, args) => {

        // Recieve Discord Commands
        switch (name) {
            case 'spawn':
                $discord.currentInterface = $interface();
            break;
        }
    }
};

// Create Interface
var $interface = function () {

    // Define object
    let x = {
        terminal: child_process.spawn('screen'),
        send: (data) => {

            // Send Input to Terminal
            x.process.stdin.write(data + '\n');
        },
        receive: (data) => {

            // Send Output to Discord
            $discord.send(data);
        }
    };

    // Process Output
    x.terminal.on('stdout', (data) => {
        x.receive(data);
    });

    // Process Errors
    x.terminal.on('stderr', (error) => {
        x.receive(`**Error:**\n${error}`);
    });

    // Return
    return x;
};

问题在于创建终端本身。如何在子进程中创建 SSH 样式的 shell?

【问题讨论】:

  • 欢迎来到 SO!这很挑剔,但在您的模型快结束时,您在上一个 x.terminal.on 回调中有一个错字。应该是x.receive(`**Error:**\n${error}`);
  • 这里似乎存在一些障碍,例如不同的操作系统以及可用的 shell。除非你只想要一个给定 shell 的 linux 解决方案。
  • 那里,修正了错字!
  • 是的,我使用的是 CentOS 7
  • 嗯,这是给任何人获取 SSH 的权利吗?所有用户都在 CentOS 7 上吗?除非我误解了这里的用例。

标签: javascript node.js linux centos


【解决方案1】:

我会查看child_process.execFile 的文档。有一个设置 shell 的选项,但默认情况下它是禁用的。

如果您想尝试设置批处理脚本,还有this 方法。这是为 windows 设置的,答案不是为传递参数设置的,但你应该能够相当容易地适应它。

【讨论】:

    【解决方案2】:

    在意识到我真的是个白痴之后,我找到了解决方案......

    // Import Modules
    const fs = require('fs');
    const child_process = require('child_process');
    
    // Create Interface
    var interface = {
        terminal: child_process.spawn('/bin/sh'),
        handler: console.log,
        send: (data) => {
            interface.terminal.stdin.write(data + '\n');
        },
        cwd: () => {
            let cwd = fs.readlinkSync('/proc/' + interface.terminal.pid + '/cwd');
            interface.handler({ type: 'cwd', data: cwd });
        }
    };
    
    // Handle Data
    interface.terminal.stdout.on('data', (buffer) => {
        interface.handler({ type: 'data', data: buffer });
    });
    
    // Handle Error
    interface.terminal.stderr.on('data', (buffer) => {
        interface.handler({ type: 'error', data: buffer });
    });
    
    // Handle Closure
    interface.terminal.on('close', () => {
        interface.handler({ type: 'closure', data: null });
    });
    

    用法...

    interface.handler = (output) => {
        let data = '';
        if (output.data) data += ': ' + output.data.toString();
        console.log(output.type + data);
    };
    
    interface.send('echo Hello World!');
    // Returns: data: Hello World!
    
    interface.send('cd /home');
    interface.cwd();
    // Returns: cwd: /home
    
    interface.send('abcdef');
    // Returns: error: bin/sh: line 2: abcdef: command not found
    
    interface.send('exit');
    // Returns: exit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-22
      • 2019-01-20
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      相关资源
      最近更新 更多