Skip to content

Instantly share code, notes, and snippets.

@missinglink
Forked from katanacrimson/netstat.js
Created May 4, 2013 00:41
Show Gist options
  • Save missinglink/5515436 to your computer and use it in GitHub Desktop.
Save missinglink/5515436 to your computer and use it in GitHub Desktop.
var child = require('child_process'),
util = require('util'),
netstat = child.spawn('netstat', ['-nb']),
out = ''
netstat.stdout.on('data', function(data) {
out += data
})
netstat.stderr.on('data', function(data) {
console.log('err: ' + data)
})
netstat.on('exit', function(code) {
var i, bucket = [], processes = {}
if(code !== 0) {
console.log('!!! exited, status code: ' + code)
return
}
// parse & serialize netstat output
out = out.split(/\n/)
// drop the first three lines...
for(i = 0; i <= 3; i++) out.shift()
out.forEach(function(entry) {
if(!entry) return
entry = entry.replace(/^\s\s*/, '').replace(/\r/, '').replace('Can not obtain ownership information', '[???]').split(/[ ]+/)
if(entry[0][0] == '[') {
i = entry[0].replace(/^\[([^\]]+)\]$/, '$1')
if(processes[i] === undefined) processes[i] = []
processes[i] = processes[i].concat(bucket)
bucket = []
} else {
bucket.push({
conntype:entry[0],
source:entry[1],
dest:entry[2],
//status:entry[3],
})
}
})
out = JSON.stringify(processes)
// READY FOR DISPATCH! *salute*
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment