Bulk Upload users into LDAP Directory

In this post I am not going to talk about setting up your LDAP using OpenLDAP. There are lots of websites explaining that. However when I tried to upload users in bulk into the directory I was not able to do the same. The reason there was not even a single article which could give me some information on the same. So here is how to do it.

Environment: Ubuntu 10.04 Server OS, OpenLDAP with phpldapadmin.

I had the user list with the first name and the last name and the email address. I converted this spreadsheet into a .CSV file.

# sudo su - (only for convenience)
 
# cat user-file.csv

Krishnan, Sethuraman, krishnan@mydomain.com
Anidha, Krishnan, anidha@mydomain.com
Tanya, Krishnan, tanya@mydomain.com

My requirement was to populate the following attributes in the LDAP.

dn: uid=krishnan,ou=people,o=mycompany
changetype: add
objectClass: inetOrgPerson
objectclass: top
cn: Krishnan Sethuraman
sn: Sethuraman
uid: krishnan
mail: krishnan@mydomain.com
userPassword:
givenName: Krishnan

I want the password to be in SHA, so I do the following first.

# slappasswd -h {SHA}
new password:
Re-enter new password:
{SHA}iQ55k3YmnmWt/VyJXIRbY

I will be using this as the password for the users.

Now I want to retrieve the values that I require from the user-file.csv file. For this I performed the following tasks.

# cat user-file.csv

# cat user-file.csv | awk -F "@" '{print $1}'

This command displayed all the values other than the @mydomain.com. I do not want to populate @mydomain.com in the ldif file. Then I import these results into a file.

# cat user-file.csv | awk -F "@" '{print $1}' > temp-user-file.csv

# cat temp-user-file.csv


Krishnan, Sethuraman, krishnan
Anidha, Krishnan, anidha
Tanya, Krishnan, tanya

# cat temp-user-file.csv | awk -F "," '{print $1 " " $2 " " $3}'

The above command gave the following results.

Krishnan Sethuraman krishnan
Anidha Krishnan anidha
Tanya Krishnan tanya

After confirming the above result was correct, I went ahead and converted the csv file into the ldif with the format that I wanted and the for that I typed the following command. 

# cat temp-user-file.csv | awk -F "," '{print "dn: uid="$3",ou=people,o=mycompany\nobjectClass: inetOrgPerson\nobjectclass: top\ncn: "$1" " " "$2"\nsn: "$2"\nuid: "$3"\nmail: "$3"@mycompany.com\nuserPassword: {SHA}iQ55k3YmnmWt/VyJXIRbY=\ngivenName: "$1"\n"}'

The above command gave me the following command.

dn: uid=krishnan,ou=people,o=mycompany
objectClass: inetOrgPerson
objectclass: top
cn: Krishnan Sethuraman
sn: Sethuraman
uid: krishnan
mail: krishnan@mycompany.com
userPassword: {SHA}iQ55k3YmnmWt/VyJXIRbY=
givenName: Krishnan

dn: uid=anidha,ou=people,o=mycompany
objectClass: inetOrgPerson
objectclass: top
cn: Anidha Krishnan
sn: Krishnan
uid: anidha
mail: anidha@mycompany.com
userPassword: {SHA}iQ55k3YmnmWt/VyJXIRbY=
givenName:Anidha

dn: uid=tanya,ou=people,o=mycompany
objectClass: inetOrgPerson
objectclass: top
cn: Tanya Krishnan
sn: Krishnan
uid: tanya
mail: tanya@mycompany.com
userPassword: {SHA}iQ55k3YmnmWt/VyJXIRbY=
givenName: Tanya

I was satisfied with this result. Hence I now move the output into the ldif file of my choice.

# cat temp-user-file.csv | awk -F "," '{print "dn: uid="$3",ou=people,o=mycompany\nobjectClass: inetOrgPerson\nobjectclass: top\ncn: "$1" " " "$2"\nsn: "$2"\nuid: "$3"\nmail: "$3"@mycompany.com\nuserPassword: {SHA}iQ55k3YmnmWt/VyJXIRbY=\ngivenName: "$1"\n"}' > user-ldap-upload.ldif

Once I had the ldif file I then entered these values into the LDAP directory with the following command.

# ldapadd -a -D "cn=ldapadmin,o=mycompany" -w ldapadminpassword -H ldapi:/// -f user-ldap-upload.ldif

Success.

Comments

  1. Thank you for your tremendous help in our new project. Without your diligence, hard work, late nights, and early mornings we wouldn’t have been able to meet the deadline. Excellent work!
    network services

    ReplyDelete

Post a Comment

Popular posts from this blog

Learning Zabbix