Skip to content

Instantly share code, notes, and snippets.

@alexzorin
Created July 30, 2013 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexzorin/6109681 to your computer and use it in GitHub Desktop.
Save alexzorin/6109681 to your computer and use it in GitHub Desktop.
Mass IP address country lookup counter. This reads IP addresses off `stdin`, gives them a country classification and outputs the the count as a JSON object to `stdout`. Uses GeoIP-Lite free (but not very accurate) database for the lookup.
var geoip = require("geoip-lite"),
Lazy = require("lazy"),
Chart = require("cli-chart");
var data = {};
new Lazy(process.stdin)
.on("end", function() {
console.log(JSON.stringify(data));
})
.lines
.forEach(function(line) {
var geo = geoip.lookup(line.toString("utf-8"));
if(geo !== null) {
data[geo.country] = data[geo.country] + 1 || 1;
}
});
@alexzorin
Copy link
Author

For example,

cat ip_list.txt | node classify.js
{"US":13242,"IN":23265,"TW":6269,"AU":1422,"TH":15101,"JP":1862,"ID":11572,"BD":473,"CN":652,"PK":7588,"VN":11820,"LA":60,"HK":915,"NZ":307,"GU":56,"MN":633,"PH":10684,"MY":3310,"SG":695,"FJ":173,"KH":324,"AF":134,"LK":225,"NP":169,"BN":60,"PG":22,"MA":1495,"NA":65,"ZA":562,"SD":942,"KR":701,"CA":1375,"BS":79,"FR":1561,"RO":3583,"AL":723,"UA":3833,"BG":1030,"BA":1686,"HU":388,"RS":2347,"JO":1145,"PL":4064,"ES":4095,"NL":831,"IR":27577,"LB":609,"CY":105,"AD":16,"RU":3114,"IT":2267,"LU":27,"SE":180,"BY":967,"IQ":1619,"AZ":1532,"BE":305,"GB":1801,"IL":1719,"BH":207,"KZ":6210,"CH":127,"GR":995,"SI":218,"MD":1113,"NO":118,"DE":2234,"YE":743,"KG":1045,"MQ":5,"UZ":262,"CZ":345,"HR":1041,"ME":267,"TR":21157,"SK":266,"AT":257,"GE":1407,"IE":205,"PT":369,"AM":1530,"TJ":57,"PS":1178,"SA":2559,"VU":34,"AP":4,"PF":42,"NC":54,"MO":75,"MV":221,"MH":7,"WF":3,"BT":13,"FM":6,"MM":18,"EU":265,"SY":1271,"DK":72,"MX":22707,"PR":550,"LV":154,"KW":745,"AE":1729,"OM":705,"MK":1464,"CL":2591,"EC":5592,"IS":23,"VE":3562,"NI":150,"BW":64,"CO":6421,"AR":9604,"GT":1538,"SV":471,"PY":180,"JM":339,"TO":14,"QA":302,"EE":92,"BR":7879,"LT":342,"A2":58,"GI":10,"TL":32,"PE":19558,"PA":364,"TT":164,"DO":847,"HT":157,"BO":1281,"CR":230,"HN":347,"UY":851,"BZ":24,"FI":56,"AN":23,"GY":26,"CU":4,"SR":15,"DZ":4176,"BI":7,"TD":1,"EG":3836,"GQ":6,"GP":7,"TN":1488,"MT":82,"GL":11,"SM":1,"KE":120,"CM":103,"MC":15,"UG":64,"KN":23,"MG":23,"MU":258,"SN":256,"ER":2,"NG":421,"ML":22,"DJ":19,"GH":334,"CI":62,"LS":11,"MW":23,"AO":263,"MZ":72,"BF":22,"TZ":106,"RW":41,"CD":37,"BJ":23,"GA":34,"ZM":58,"GM":20,"CG":14,"GN":3,"ZW":49,"ET":161,"SL":21,"SO":3,"ST":4,"CF":3,"KM":4,"BM":11,"AG":23,"VC":27,"A1":9,"BB":48,"AW":25,"SB":6,"PW":1,"TV":1,"KI":2,"CK":1,"AS":9,"MP":17,"AI":4,"LC":40,"VI":24,"DM":2,"KY":7,"MF":1,"LY":672,"VG":2,"FO":3,"LI":8,"IM":1,"GF":1,"GD":15,"TM":1,"NE":5,"MR":46,"LR":33,"SC":9,"SZ":11,"TG":18,"RE":3,"CV":44,"TC":2,"GG":2,"JE":5}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment