I am trying to run the following shell command:
netstat -nat | grep 3000
and eventually
netstat -nat | grep 3000 | grep ESTABLISHED
from Node.js to obtain ip address connected to specific port using spawn
according to https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options visible below:
const netStat = spawn('netstat', ['-nat']);
const grep = spawn('grep', ['3000']);
netStat.stdout.on('data', (data) => {
grep.stdin.write(data);
});
console.log('Determining public ip\'s connected to port 3000');
grep.stdout.on('data', function(data){
console.log(data.toString());
});
But the result just hangs, am I doing something wrong? Thanks in advance for the help!
Since you don't read from the stderr
pipe and don't ignore stdio
using the options, the process hangs because the pipe is still open until read from.
A similar, but unrelated issue is Why is my Node child process that I created via spawn() hanging? .