#!/usr/bin/python import pwd, sys, os, csv def main(argv): try: fullDN = 'ou=people,dc=mydomain,dc=com' rootDN='cn=Manager,dc=mydomain,dc=com' ldifFile = 'converted.ldif' fd = csv.reader(open('shadow','r'), delimiter=':') ldifOut = open(ldifFile, 'w+') pwLine = [] for line in fd: if line[1] in '*LK*' 'NP' '*' '!!': continue String = pwd.getpwnam(line[0]) pwLine = list(String) index = pwLine.index('x') pwLine.pop(index) pwLine.insert(index, line[1]) ldifOut.write('dn: cn=' + pwLine[0] + ',' + fullDN + '\n') ldifOut.write('cn: ' + pwLine[0] + '\n') ldifOut.write('uid: ' + pwLine[0] + '\n') ldifOut.write('userPassword: ' + pwLine[1] + '\n') ldifOut.write('uidNumber: ' + str(pwLine[2]) + '\n') ldifOut.write('gidNumber: ' + str(pwLine[3]) + '\n') ldifOut.write('homeDirectory: ' + pwLine[5] + '\n') ldifOut.write('gecos: ' + pwLine[4] + '\n') ldifOut.write('loginShell: ' + pwLine[6] + '\n') ldifOut.write('sn: ' + pwLine[4] + '\n') ldifOut.write('objectClass: posixAccount\n') ldifOut.write('objectClass: shadowAccount\n') ldifOut.write('objectClass: inetorgperson\n') ldifOut.write('\n') except IOError, (errno, strerror): sys.exit('I/O error (%s): %s' % (errno, strerror)) except ValueError: sys.exit('Could not find x in string %s: %s' % (pwLine, sys.exc_info()[0])) ldifOut.close() updateLDAP(ldifFile, rootDN, '127.0.0.1') #end of the main method def updateLDAP(ldifFileName, baseDN, ldapServerIP): import subprocess ldapCmd = '/usr/bin/ldapadd' ldapArgs = ' -w secret -x -D ' + baseDN + ' -f ' + ldifFileName ldapStr = ldapCmd + ldapArgs try: output = subprocess.Popen(ldapStr, shell=True, stdout=subprocess.PIPE) output.wait() stdout_value = output.communicate()[0] except IOError, (errno, strerror): sys.exit('I/O error (%s): %s' % (errno, strerror)) #end of the updateLDAP method if __name__ == '__main__': main(sys.argv[1:])