使用 node.js 执行终端命令
execute a terminal command using node.js
我正在尝试使用 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.
如何在没有字符串
的情况下使用>
您可以使用文件流将生成的 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
这里是一个流,您可以将它通过管道传输到您记录的文件流中。
我正在尝试使用 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.
如何在没有字符串
的情况下使用>
您可以使用文件流将生成的 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
这里是一个流,您可以将它通过管道传输到您记录的文件流中。