Skip to content

Instantly share code, notes, and snippets.

@Deanout
Created March 21, 2018 00:51
Show Gist options
  • Save Deanout/d885bb8471781e5d3f2d255f9314750b to your computer and use it in GitHub Desktop.
Save Deanout/d885bb8471781e5d3f2d255f9314750b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Adds the given users to LDAP, provided via command line.
# Accepts multiple users.
users=()
passwds=()
for i in "$@"
do
parsed=(${i//:/ })
users+=(${parsed[0]})
passwds+=(${parsed[1]})
done
ldif_file="/tmp/bulk_user.ldif"
dn="cn=admin,dc=csi3670,dc=local"
adminpw="<ENTER YOUR ADMIN PASSWORD>"
i=0
for user in "${users[@]}"; do
userpw=${passwds[$i]}
touch $ldif_file
uid=$(( $i + 1000 ))
gid=$(( $i + 1000 ))
echo $uid $gid
echo "Adding $user to LDAP directory with UID [$uid] and GID [$gid]"
echo "dn: uid=$user,ou=People,dc=csi3670,dc=local" >> $ldif_file
echo "objectClass: inetOrgPerson" >> $ldif_file
echo "objectClass: posixAccount" >> $ldif_file
echo "objectClass: shadowAccount" >> $ldif_file
echo "uid: $user" >> $ldif_file
echo "sn: 1" >> $ldif_file
echo "givenName: User" >> $ldif_file
echo "cn: User $i" >> $ldif_file
echo "displayName: User $i" >> $ldif_file
echo "uidNumber: $uid" >> $ldif_file
echo "gidNumber: $gid" >> $ldif_file
echo "userPassword: $userpw" >> $ldif_file
echo "gecos: User $i" >> $ldif_file
echo "loginShell: /bin/bash" >> $ldif_file
echo "homeDirectory: /home/$user" >> $ldif_file
cat $ldif_file
# Add user
ldapadd -x -D $dn -w $adminpw -a -f $ldif_file
# Clean up
rm $ldif_file
i=$(( $i + 1 ))
done
#!/bin/bash
# Adds a single user to LDAP via cmd line arguments
user=${1}
userpw=${2}
ldif_file="/tmp/bulk_user.ldif"
dn="cn=admin,dc=csi3670,dc=local"
adminpw="<ENTER YOUR ADMIN PASSWORD>"
i=1
touch $ldif_file
uid=$((1000 ))
gid=$((1000 ))
echo $uid $gid
echo "Adding $user to LDAP directory with UID [$uid] and GID [$gid]"
echo "dn: uid=$user,ou=People,dc=csi3670,dc=local" >> $ldif_file
echo "objectClass: inetOrgPerson" >> $ldif_file
echo "objectClass: posixAccount" >> $ldif_file
echo "objectClass: shadowAccount" >> $ldif_file
echo "uid: $user" >> $ldif_file
echo "sn: 1" >> $ldif_file
echo "givenName: User" >> $ldif_file
echo "cn: User $i" >> $ldif_file
echo "displayName: User $i" >> $ldif_file
echo "uidNumber: $uid" >> $ldif_file
echo "gidNumber: $gid" >> $ldif_file
echo "userPassword: $userpw" >> $ldif_file
echo "gecos: User $i" >> $ldif_file
echo "loginShell: /bin/bash" >> $ldif_file
echo "homeDirectory: /home/$user" >> $ldif_file
cat $ldif_file
# Add user
ldapadd -x -D $dn -w $adminpw -a -f $ldif_file
# Clean up
rm $ldif_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment