Skip to content

Instantly share code, notes, and snippets.

@moqada
Created January 30, 2013 09:34
Show Gist options
  • Select an option

  • Save moqada/4671962 to your computer and use it in GitHub Desktop.

Select an option

Save moqada/4671962 to your computer and use it in GitHub Desktop.
Add LDAP user by csv
# -*- coding: utf-8 -*-
import os
import csv
tmpl = open('user.ldif').read()
fh_csv = open('users.csv')
PASSWD = os.environ['LDAP_PASSWD']
def add_user(row):
body = tmpl % {
'id': row[0],
'uid': row[1],
'displayName': row[2],
'givenName': row[3],
'sn': row[4],
'userPassword': row[5]
}
filename = 'user-%s.ldif' % row[0]
fh = open(filename, 'w')
fh.write(body)
fh.close()
os.system(
'ldapadd -x -w %s -D "cn=Manager,dc=example,dc=com" -f %s' % (
PASSWD, filename))
print 'Added %s' % row[0]
if __name__ == '__main__':
reader = csv.reader(fh_csv)
for row in reader:
add_user(row)
print 'Complete!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment