Search code examples
bashnode.jstty

pushing tty output to node


For the satisfaction of my curiosity gene i'd like to play with bash/node combo. I don't know how to make those 2 talk together. I just had a great smile on my face finding about TTY.JS ;-)

How do I feed terminal's output (sdtout?) to node? I thought about redirecting the stream to file and read it with node through 'fs' module. But there must be some prettier way I bet

thanks in advance.


Solution

  • Something like this should send terminal output to node

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

    Hope this helps