Skip to content

Instantly share code, notes, and snippets.

@tangmi
Last active August 29, 2015 13:56
Show Gist options
  • Save tangmi/8856788 to your computer and use it in GitHub Desktop.
Save tangmi/8856788 to your computer and use it in GitHub Desktop.
streaming parser for fake-ssh logins file
/*
* Takes a tab separated file from tylermenezes' fake-ssh:
*
* 1380751629 username tylermenezes
* 1380751629 password foo
* 1380751634 a_password None
* 1380751634 username tylermenezes
* 1380751634 password foobar
* 1380751638 a_password None
* ...
*
* Usage:
*
* node index.js > passwords.csv
*
*/
var fs = require('fs');
var rst = fs.createReadStream("logins");
rst.setEncoding('utf8');
var START_LINE_POS = 11; //ignore timestamp
var passwords = {};
var line = [];
rst.on('data', function(chunk) {
var linePos = 0;
for (var i = 0, l = chunk.length; i < l; i++) {
var c = chunk[i];
if (c == '\n') {
parseLine(line);
linePos = 0;
line = [];
} else {
if (linePos >= START_LINE_POS) {
line.push(c);
}
linePos++;
}
}
});
rst.on('end', function() {
printCsv(passwords);
rst.close();
});
function parseLine(arr) {
if (arr[0] == 'p') {
//password
var password = arr.slice(9).join('');
passwords[password] |= 0;
passwords[password]++;
}
}
function printCsv(obj) {
for (var key in obj) {
//csv format for dealing with commas
var escaped = ['"', key.replace(/"/g, '""'), '"'].join('');
console.log([escaped, obj[key]].join(','))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment