remove_member 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. username=$1
  3. group=$2
  4. current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  5. ini="$current_dir/scripts.ini"
  6. ldap_password=$(awk '/ldap_password/ {print $2}' $ini)
  7. dc=$(awk '/dc/ {print $2}' $ini)
  8. cn="$(awk '/cn/ {print $2}' $ini),$dc"
  9. group_ou="$(awk '/group_ou/ {print $2}' $ini),$dc"
  10. users_ou="$(awk '/users_ou/ {print $2}' $ini),$dc"
  11. ldiftmp="/tmp/ldapmemberremove.ldif"
  12. ldiftemplate="$current_dir/remove_member.ldif"
  13. if [ ! -f $ldiftemplate ]
  14. then
  15. echo "Template file not found in $ldiftemplate"
  16. exit 0
  17. fi
  18. if [ -z "$username" ]
  19. then
  20. echo "Empty username. usage: add_member 'username' 'group'"
  21. exit 0
  22. fi
  23. if [ -z "$group" ]
  24. then
  25. echo "Empty group. usage: add_member 'username' 'group'"
  26. exit 0
  27. fi
  28. TEXT=$(cat $ldiftemplate)
  29. TEXT=${TEXT//\{LOGIN\}/$username}
  30. TEXT=${TEXT//\{GROUP\}/$group}
  31. TEXT=${TEXT//\{GROUP_OU\}/$group_ou}
  32. TEXT=${TEXT//\{PEOPLE_OU\}/$users_ou}
  33. echo "$TEXT" > $ldiftmp
  34. ldapmodify -D $cn -w $ldap_password -f $ldiftmp > /dev/null 2>&1
  35. if [ $? -gt 0 ]
  36. then
  37. echo 'Error'
  38. else
  39. echo 'Success!'
  40. fi
  41. rm $ldiftmp