javascript - 如何在 NodeJS 子进程中创建终端实例?

标签 javascript node.js linux centos

我正在设置一个不和谐 channel 作为 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?

最佳答案

After realizing how much of an idiot I really am, I found a solution...

// 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 });
});

Usage...

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

关于javascript - 如何在 NodeJS 子进程中创建终端实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185655/

相关文章:

linux - BASH:输入期间的 Ctrl+C 会中断当前终端

linux - 我如何在终端中回显字符串+命令

javascript - 如何将 jquery 模板文件保存为每个文件中有多个模板的外部模板文件?

javascript - 如何在 D3.JS 中使用圆内的 defs 元素

javascript - Material-UI:提供给 createMuiTheme 的阴影数组应该支持 25 个高度

node.js - 使用 AWS 服务持续交付 NodeJS 应用程序

javascript - nodejs导入需要转换

javascript - 无法发送具有对象数组属性的对象

javascript - 如何使用cheerio用一个标签包装元素的子元素/子元素?

c++ - 将 Crypto++ 代码集成到 Linux 中的 Qt 应用程序