【发布时间】:2019-10-11 14:03:29
【问题描述】:
我有这个:
const net = require('net');
const cp = require('child_process');
const server = net.createServer(s => {
const bash = cp.spawn('bash');
s.on('data', d => {
bash.stdin.write(`
exec ${String(d)};
echo 'write to socket s here'; // write to s here
`);
});
bash.stdout.pipe(s, {end:false});
bash.stderr.pipe(s, {end:false});
});
server.listen(4004);
有谁知道我如何从 bash 进程中写入套接字?我假设我必须以某种方式将文件描述符编号传递给 bash?如何获取套接字的文件描述符?我在控制台记录了套接字属性,但没有看到文件描述符。
类似这样的:
const fd = s.fd;
const bash = cp.spawn('bash', [], {
env: Object.assign({}, process.env, {
socketfd: fd
})
});
然后
bash.stdin.write(`
exec ${String(d)};
echo "test" >&"$socketfd"
`)
【问题讨论】:
标签: node.js shell sockets tcp file-descriptor