Node.js Netstat Utility

1.9.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `node-netstat` to asynchronously retrieve and filter network connection data. It shows how to apply filters for PID and protocol, limit the output, handle errors with the `done` callback, and conditionally stop processing results early from within the line handler function.

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
  }
});

view raw JSON →