使用node.js执行终端命令

时间:2015-02-10 07:45:10

标签: abc

尝试使用node.js spawn

执行终端命令

表示使用代码

console.log(args)

var child = spawn("hark", args, {cwd: workDir});
        child.stdout.on('data', function(data) {

          console.log(data.toString())        
        });

        child.stderr.on('data', function(data) {
          console.log('stdout: ' + data);

        });

        child.on('close', function(code) {
          console.log('closing code: ' + code);

        });

But it treated greater than> as string">"和 获得输出

tshark: Invalid capture filter "> g.xml" 

    That string isn't a valid capture filter (syntax error).
    See the User's Guide for a description of the capture filter syntax.

如何使用>无字符串

1 个答案:

答案 0 :(得分:2)

您可以使用文件流将所有生成的hark输出到g.xml 例如:

// don't need ">" in args
var args = [' 02:00:00:00' ,'-s','pdml'],
    logStream = fs.createWriteStream('./xml');

var spawn = require('child_process').spawn,
    child = spawn('tshark', args);

child.stdout.pipe(logStream);
child.stderr.pipe(logStream);

child.on('close', function (code) {
    console.log('child process exited with code ' + code);
});

由于child这是一个流,您只需将其传输到已记录的文件流中即可。