簡體   English   中英

如何在NodeJS子進程中創建終端實例?

[英]How do you create a terminal instance within a NodeJS child process?

我正在設置一個discord通道以用作SSH終端。 NodeJS服務器將提供連接。 定制命令將產生一個新的終端實例,然后可以將其用作外殼程序。

我不知道如何在子進程中生成終端。 我嘗試使用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?

我來看看child_process.execFile的文檔。 有一個選項可以設置外殼,但是默認情況下它是禁用的。

如果您想嘗試設置批處理腳本,也可以使用這種方法。 這是為Windows設置的,而答案不是為傳遞參數而設置的,但是您應該能夠輕松地對其進行調整。

在意識到我到底是個白痴之后,我找到了解決方案...

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM