Last active
December 19, 2015 04:59
-
-
Save enko/5900851 to your computer and use it in GitHub Desktop.
Holt die verbundenen MAC-Adressen und versucht die Hersteller rauszufinden.
This file contains hidden or 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
node_modules |
This file contains hidden or 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
var http = require('http'); | |
var Table = require('cli-table'); | |
var pg = require('pg'); | |
var moment = require('moment'); | |
Object.size = function(obj) { | |
var size = 0, key; | |
for (key in obj) { | |
if (obj.hasOwnProperty(key)) size++; | |
} | |
return size; | |
}; | |
function fetchJSON(url, cb) { | |
http.get(url,function (res) { | |
var body = ''; | |
res.on('data', function (chunk) { | |
body += chunk.toString(); | |
}); | |
res.on('end', function () { | |
//console.log(body); | |
var data = undefined; | |
if (body == 'none') { | |
data = {}; | |
} else { | |
data = JSON.parse(body); | |
} | |
cb(data); | |
}); | |
}).on('error', function (e) { | |
console.log("Got error: " + e.message); | |
}); | |
} | |
var conString = "postgres://ffjstats:123456@localhost/ffjstats"; | |
var nodes_url = 'http://map.freifunk-jena.de/ffmap/nodes.json'; | |
var results = {}; | |
var table = new Table({ | |
head: ['Vendor', 'Count'] | |
}); | |
fetchJSON(nodes_url, function (data) { | |
var nodes = data.nodes.filter(function (node) { | |
return node.flags.client == true; | |
}); | |
nodes.forEach(function (node) { | |
var mac = node.macs.replace(/:/g, ''); | |
var api = "http://www.macvendorlookup.com/api/BQEVzYH/" + mac; | |
fetchJSON(api, function (mac_data) { | |
if (Object.keys(mac_data).length == 0) { | |
if ('unknown' in results) { | |
results['unknown']++; | |
} else { | |
results['unknown'] = 1; | |
} | |
} else { | |
var company = mac_data[0]['company']; | |
if (company in results) { | |
results[company]++; | |
} else { | |
results[company] = 1; | |
} | |
} | |
var counter = 0; | |
Object.keys(results).forEach(function(el){ | |
counter += results[el]; | |
}); | |
if (counter == nodes.length) { | |
var client = new pg.Client(conString); | |
pg.connect(conString,function(err, client, done) { | |
if(err) { | |
return console.error('could not connect to postgres', err); | |
} | |
var dataset = []; | |
Object.keys(results).forEach(function(el){ | |
if (el != 'unknown') { | |
var date = moment().format('YYYY-MM-DD HH:mm:ss'); | |
dataset.push(['"'+el+'"',results[el],'"'+date+'"']); | |
} | |
}); | |
var stream = client.copyFrom("COPY usage (vendor,clients,insdate) FROM STDIN WITH CSV"); | |
stream.on('close', function () { | |
client.end(); | |
}); | |
stream.on('error', function (error) { | |
console.log("Sorry, error happens", error); | |
}); | |
dataset.forEach(function(el){ | |
var str = el.join(',')+"\n"; | |
stream.write(str); | |
}); | |
stream.end(); | |
}); | |
} | |
}); | |
}); | |
}); | |
This file contains hidden or 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
{ "name": "node-stats" | |
, "version": "0.0.1" | |
, "engines": ["node >= 0.8.0"] | |
, "main": "index.js" | |
, "dependencies": | |
{ "cli-table": "*", | |
"pg": "*", | |
"moment": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment