【发布时间】: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