Crawl over a ldif file and make a csv of users with a column for each group they're a member of.
This file contains 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
'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