running a ping command as a node child_process and streaming its stdout for parsing

924 views
Skip to first unread message

Bernard Brault

unread,
Nov 12, 2013, 11:12:18 PM11/12/13
to nod...@googlegroups.com
I am running a linux ping command as a child process using the node.js code below.

The ping command has to be continuous and normally displays the ping result every second when running in a console.
The last line of code simply pipes the output to the console as if the command had been issued on a console.

I need to parse the ping results as they come in in node.js.
Possible Solution : I think that this can possibly be done by creating a duplex stream, piping the "ping output" to the duplex stream and reading  on an "available" event.

I cannot get the above working! The first problem I run into is the lack of documentation on creating an instance for a duplex stream.

==


var exec = require('child_process').exec,
    child;

child = exec('ping  192.168.1.82',
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
            console.log('exec error: ' + error);
        }
    });
child.stdout.pipe(process.stdout);

mscdex

unread,
Nov 13, 2013, 8:48:56 AM11/13/13
to nod...@googlegroups.com
On Tuesday, November 12, 2013 11:12:18 PM UTC-5, Bernard Brault wrote:
I am running a linux ping command as a child process using the node.js code below.


FWIW you can do ping in node without a child process: https://npmjs.org/package/net-ping
Reply all
Reply to author
Forward
0 new messages