Created
April 16, 2016 18:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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