admin scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.8 KiB

  1. #!/bin/bash
  2. # Quick ldap search tool
  3. # ver 0.1 sonic - I will never ever update this
  4. # ver 0.2 koffee - quick update & shell checking
  5. # ver 0.3 butlerx - add support for dcu AD
  6. # ver 0.4 butlerx - support for more flags and multiple searches at once
  7. SCRIPTNAME="rbsearch"
  8. EMAIL="bill.gates@hotmail.com"
  9. USERNAME="microbg"
  10. FULLNAME="Bill Gates"
  11. VERSION="0.4"
  12. SEARCH="ldapsearch -D cn=root,ou=ldap,o=redbrick -xLLL -y /etc/ldap.secret"
  13. function dcuSearch () {
  14. DCUSEARCH=""
  15. for i in "$@"; do
  16. DCUSEARCH="$DCUSEARCH $i"
  17. done
  18. echo "======== DCU AD search results ========"
  19. ldapsearch -x -h ad.dcu.ie -D "CN=rblookup,OU=Service Accounts,DC=ad,DC=dcu,DC=ie" -w "$(</etc/ldap_dcu.secret)" -b "OU=Students,DC=ad,DC=dcu,DC=ie" -s sub "($DCUSEARCH)" cn gecos mail | awk '/cn:|gecos:|mail:/ { print }'
  20. echo "======================================="
  21. }
  22. function usage () {
  23. echo "Usage: $SCRIPTNAME [--mail | --altmail] [-u | --uid | --user] [--id] [-f | --fullname | --name] [-nick] [-i | info] [--dcu [-u | --username]| [-f | --fullname]] query"
  24. echo "eg. $SCRIPTNAME --altmail $EMAIL"
  25. echo " $SCRIPTNAME --uid $USERNAME"
  26. echo " $SCRIPTNAME --fullname $FULLNAME"
  27. echo " $SCRIPTNAME --info [ $FULLNAME | $EMAIL ]"
  28. echo " $SCRIPTNAME --dcu --username $USERNAME"
  29. echo " $SCRIPTNAME --dcu --fullname $FULLNAME"
  30. echo ""
  31. echo "Only fullname takes 2 arguments."
  32. echo "All results will give just uid(s) apart from [-uid]"
  33. echo "[-uid] prints all ldap info for that uid"
  34. }
  35. while [[ $# -gt 1 ]]; do
  36. key="$1"
  37. case $key in
  38. -update|--update|--info|-i)
  39. if [ ! -z "$3" ]; then
  40. echo "==== LDAP Search for $2 $3 ===="
  41. $SEARCH "cn=*$2*$3" uid yearsPaid loginShella &&
  42. echo "======================================="
  43. shift; shift
  44. elif [ ! -z "$2" ]; then
  45. echo "==== LDAP Search for $2 ===="
  46. $SEARCH "altmail=$2" uid yearsPaid loginShell &&
  47. echo "======================================="
  48. shift
  49. else
  50. echo "Usage: $SCRIPTNAME --info [ $FULLNAME | $EMAIL ]"
  51. fi
  52. shift
  53. ;;
  54. -altmail|--mail|--altmail)
  55. echo "==== LDAP Search for $2 ===="
  56. $SEARCH "altmail=$2" uid altmail &&
  57. echo "======================================="
  58. shift
  59. ;;
  60. -uid|--user|-u|--uid)
  61. echo "==== LDAP Search for $2 ===="
  62. $SEARCH "uid=$2" &&
  63. echo "======================================="
  64. shift
  65. ;;
  66. -id|--id)
  67. echo "==== LDAP Search for $2 ===="
  68. $SEARCH "id=$2" uid id &&
  69. echo "======================================="
  70. shift
  71. ;;
  72. -nick|--nick|-n)
  73. echo "==== LDAP Search for $2 ===="
  74. $SEARCH "gecos=*$2*" uid gecos &&
  75. echo "======================================="
  76. shift
  77. ;;
  78. -fullname|-name|-f|--name|--fullname)
  79. if [ ! -z "$3" ]; then
  80. echo "==== LDAP Search for $2 $3 ===="
  81. $SEARCH "cn=*$2*$3" uid cn &&
  82. echo "======================================="
  83. shift; shift
  84. else
  85. echo "Usage: $SCRIPTNAME --fullname $FULLNAME"
  86. fi
  87. ;;
  88. -newbie|--newbie|--noob)
  89. echo "======= LDAP Search for newbies ======="
  90. $SEARCH "newbie=TRUE" uid &&
  91. echo "======================================="
  92. ;;
  93. --dcu)
  94. case $2 in
  95. --username|-u)
  96. SEARCHARG="cn=$3"
  97. dcuSearch "$SEARCHARG" &&
  98. shift
  99. ;;
  100. --fullname|-f)
  101. if [ ! -z "$4" ]; then
  102. SEARCHARG="displayName=$3 $4"
  103. dcuSearch "$SEARCHARG" &&
  104. shift; shift
  105. else
  106. echo "Usage: $SCRIPTNAME --dcu --fullname $FULLNAME"
  107. shift
  108. fi
  109. ;;
  110. *)
  111. usage
  112. ;;
  113. esac
  114. shift
  115. ;;
  116. -h|--help)
  117. usage
  118. ;;
  119. -v|--version)
  120. echo Version $VERSION
  121. ;;
  122. *)
  123. usage
  124. ;;
  125. esac
  126. shift
  127. done