Skip to content

Instantly share code, notes, and snippets.

@par6n
Created Apr 16, 2016
Embed
What would you like to do?
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