Skip to content

Instantly share code, notes, and snippets.

@par6n
Created April 16, 2016 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save par6n/22373baf67b86b568cc570d87767cb04 to your computer and use it in GitHub Desktop.
Save par6n/22373baf67b86b568cc570d87767cb04 to your computer and use it in GitHub Desktop.
This is a dead simple NodeJS code that extracts netstat information and prints them out. Worked on Windows 10, 8.1 and 8
function netstat( cb ) {
const spawn = require( 'child_process' ).spawn;
const netstat = spawn( 'netstat', [ '-e' ] );
var resp = '';
netstat.stdout.on( 'data', ( data ) => {
resp += data.toString();
} );
netstat.stdout.on( 'end', ( data ) => {
cb( resp );
} );
}
var lastRec = 0, lastSent = 0;
function monitor() {
netstat( function( data ) {
var stats = data.toString(),
firstLine = /\d+(\s*)\d+/im,
seperator = /(\d+)/g;
if ( ( res1 = firstLine.exec( stats ) ) != null ) {
res1 = res1[0];
var res2 = res1.match( seperator );
receivedStats( parseInt( res2[0] ), parseInt( res2[1] ) );
}
} );
}
setInterval( monitor, 5000 );
function receivedStats( received, sent ) {
console.log( `Received: ${received / 1000000} MB\t\tSent: ${sent / 1000000} MB` );
if ( lastRec > 0 )
console.log( `Difference: ${received - lastRec}` );
lastRec = received;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment