Skip to content

Instantly share code, notes, and snippets.

@tizzo
Last active February 22, 2019 18:11
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 tizzo/68f6eef73ce1da5769f72ee8e5f3d935 to your computer and use it in GitHub Desktop.
Save tizzo/68f6eef73ce1da5769f72ee8e5f3d935 to your computer and use it in GitHub Desktop.
Crawl over a ldif file and make a csv of users with a column for each group they're a member of.
'use strict'
const fs = require('fs');
const os = require('os');
const file = fs.readFileSync('./dump.txt', 'utf8').split(os.EOL);
const people = [];
const groups = new Set();
var current_person = false;
for (const line of file) {
if (/^dn: /.test(line)) {
if (current_person) {
people.push(current_person);
}
current_person = {
user: line.substr(4).split(',')[0].substr(4),
groups: [],
}
}
if (/^memberOf: /.test(line)) {
const group = line.substr(10).split(',')[0].substr(3);
current_person.groups.push(group);
groups.add(group);
}
}
const sortedGroups = Array.from(groups).sort();
console.log('Name,' + sortedGroups.join(','));
people.forEach(person => {
process.stdout.write(`${person.user},`);
sortedGroups.forEach(group => {
process.stdout.write(`${person.groups.includes(group) ? group : ''},`);
});
process.stdout.write(os.EOL);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment