将tty输出推送到节点

时间:2012-06-25 16:52:54

标签: bash node.js tty

为了满足我的好奇心基因,我想玩bash / node组合。 我不知道如何让这两个人在一起聊天。我脸上露出灿烂的笑容,发现TTY.JS; - )

如何将终端的输出(sdtout?)提供给节点?我想过将流重定向到文件并通过'fs'模块用节点读取它。但是我必须有一些更漂亮的方式打赌

提前感谢。

1 个答案:

答案 0 :(得分:2)

这样的事情应该将终端输出发送到节点

var app = require('express').createServer(),
    io = require('socket.io').listen(app),
    sys = require('util'),
    exec = require('child_process').exec;

app.listen(4990);

io.sockets.on('connection', function(socket) {
    socket.on('console', function(command, callBack) {
        // client sends {command: 'ls -al'}
        function puts(error, stdout, stderr) {
            socket.emit('commandresult', stdout);
        }
        exec(command.command, puts);
    });
});​

希望这有帮助