Node.js Netstat Utility
node-netstat is a JavaScript utility library designed to programmatically read and parse `netstat` data on various operating systems. It is currently at version 1.9.0 and provides a callback-based API for processing network connection information line by line. The library supports filtering results by properties like PID or protocol and offers both synchronous and asynchronous execution. Key differentiators include its cross-platform compatibility (tested on Ubuntu 14.04/16.04, Windows 7, and OS X Yosemite), extensibility via custom parsers and filters, and the ability to cancel ongoing `watch` operations. While `netstat` itself is considered obsolete on some Linux systems in favor of `ss`, node-netstat provides a unified interface for accessing this data where available, making it suitable for system monitoring and debugging tools in Node.js environments.
Common errors
-
Error: spawn netstat ENOENT
cause The `netstat` command-line utility is not found in the system's PATH. This often occurs on minimal Linux installations or Windows systems where `netstat.exe` might not be readily accessible to Node.js.fixInstall the `net-tools` package on Linux (e.g., `sudo apt-get install net-tools` or `sudo yum install net-tools`) or ensure the `netstat` executable's directory is included in the system's PATH environment variable on Windows or other OS. Alternatively, verify if the `platform` option is correctly set if overriding `os.platform()`. -
Netstat is not finding a running localhost on MacOS Sonoma.
cause Specific operating system versions (like MacOS Sonoma) might have changes in `netstat` output or behavior that `node-netstat`'s parsers are not yet equipped to handle, leading to incomplete or incorrect results.fixCheck the `node-netstat` GitHub issues for known compatibility problems with your specific OS version. If no solution is available, consider contributing a fix or using a different system monitoring tool. Temporary workarounds might involve adjusting `filter` options or inspecting the raw `netstat` output to identify parsing discrepancies.
Warnings
- gotcha The `parseName` option for the Linux parser is `false` by default. If you require `processName` in your results on Linux, you must explicitly enable it by reconfiguring the parser via `netstat.parsers.linux = netstat.parserFactories.linux({ parseName: true });`.
- gotcha Returning `false` from the `handler` function will immediately stop processing any further netstat results. This is a design feature for early termination but can be a gotcha if not intended, potentially leading to incomplete data collection.
- gotcha On some modern Linux distributions, the `netstat` command-line utility is considered obsolete and `ss` is the recommended replacement. While `node-netstat` wraps the `netstat` command, its underlying reliability might be affected by system-level changes or deprecations of the `netstat` command itself. Ensure `net-tools` (which provides `netstat`) is installed on Linux systems if you encounter issues.
- gotcha When using the `watch` option for continuous monitoring, the `netstat` function returns a handler object with a `cancel()` method. Forgetting to call `cancel()` when monitoring is no longer needed can lead to resource leaks or indefinitely running processes.
Install
-
npm install node-netstat -
yarn add node-netstat -
pnpm add node-netstat
Imports
- netstat
import netstat from 'node-netstat';
const netstat = require('node-netstat'); - netstat.parsers.linux
const netstat = require('node-netstat'); netstat.parsers.linux = netstat.parserFactories.linux({ parseName: true }); - netstat.version
const netstat = require('node-netstat'); console.log(netstat.version);
Quickstart
const netstat = require('node-netstat');
// Get network connections, filtering by a specific PID and protocol,
// limiting the results to the first 5 entries.
netstat({
filter: {
pid: process.env.TARGET_PID ? parseInt(process.env.TARGET_PID) : 1, // Example PID, adjust as needed
protocol: 'tcp'
},
limit: 5,
done: (err) => {
if (err) {
console.error('Netstat command failed:', err);
return;
}
console.log('Netstat command completed.');
}
}, function (data) {
// Process each line of netstat data
console.log('Netstat item:', data);
// Example: stop processing after finding a specific state
if (data.state === 'ESTABLISHED') {
console.log('Found an ESTABLISHED connection, stopping further processing.');
return false; // Returning false stops the stream
}
});