简体   繁体   中英

Capture tshark stdout output in node.js

I'm trying to run tshark from node and retrieve the stdout output using the following code

var spawn = require('child_process').spawn,
    ts = spawn('tshark',
               ['-i wlan0 -I -R "wlan.fc.type == 0 && wlan.fc.subtype == 4" -e wlan.sa']
              );

ts.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

ts.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

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

But I get an error

stderr: Capturing on wlan0 -I -R "wlan.fc.type == 0 && wlan.fc.subtype == 4" -T fields -e wlan.sa

stderr: tshark: The capture session could not be initiated (No such device exists). Please check to make sure you have sufficient permissions, and that you have the proper interface or pipe specified.

stderr: 0 packets captured

If I run directly tshark with the arguments it works fine.

Any clue of what could be wrong ?

The way you're calling spawn now, tshark sees the arguments as one big quoted argument and it can't parse it correctly. It'd be as if you called it like:

tshark "-i wlan0 -I -R ""wlan.fc.type == 0 && wlan.fc.subtype == 4"" -e wlan.sa"

What you need to do is separate out the arguments that you're passing to spawn in to individual items in the arguments array:

ts = spawn('tshark',
           ['-i', 'wlan0', '-I', '-R', 'wlan.fc.type == 0 && wlan.fc.subtype == 4', '-e', 'wlan.sa']
          );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM