Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active August 29, 2015 14:05
Show Gist options
  • Save ernstki/d3c71f317769626207b6 to your computer and use it in GitHub Desktop.
Save ernstki/d3c71f317769626207b6 to your computer and use it in GitHub Desktop.
Query Davmail LDAP server for "LastName, FirstName" from input file and prepare output suitable for Listserv bulk subscription
#!/bin/bash
# Query an LDAP server (assuming OWA via DavMail) for "LastName, FirstName"
# given a list of names from an input file passed on the command line; output
# names and emails in a format acceptable to Listserv for bulk subscription.
#
# The input names can be copied from a Blackboard course's Tools -> Send Email
# -> All Users. These are semicolon-separated, so replace these with newlines
# using a global search-and-replace (e.g., in Vi: :%s/; \?/<CTRL+V><CTRL+M>/g,
# where <CTRL+X> is a literal keypress, holding down the Control key).
#
# Sample input file:
# ------------------
# Nock, Natalia
# Castello, Charlyn
# Arce, Altagracia
# Traina, Theresia
#
# Sample output:
# ---------------------------------------
# Natalia Nock <[email protected]>
# Charlyn Castello <[email protected]>
# Altagracia Arce <[email protected]>
# Theresia Traina <[email protected]>
#
# Names which are not uniquely matched against the LDAP directory are
# returned as "Firstname Lastname <not found>", and you'll have to do
# a bit more digging for these.
#
# Requires: http://davmail.sourceforge.net/, Java JRE, OpenLDAP
# Sample 'ldapsearch' command-line invocation: http://simp.ly/publish/JpGwwZ
#
# Author: Kevin Ernst <[email protected]>
# Date: 14 May 2014
#set -x
BIND_DN="[email protected]" # your UC webmail (OWA) login/email
BASE_DN="ou=people" # start searching directory here
LDAP_URI="ldap://localhost:1389" # DavMail's default host/port
echo "This script expects DavMail (http://davmail.org) running at $LDAP_URI."
echo
echo "It reads a list of names in 'Last, First<newline>' format (as you might"
echo "copy and paste from Blackboard's class roster) from the file specified"
echo "as the first argument and outputs names and email addresses in a format"
echo "acceptable for a bulk import into Listserv 15.x."
echo
# Bail if 'ldapsearch' isn't found in the path
if ! which ldapsearch >/dev/null; then
echo "ACK! Could not find 'ldapsearch' in your path." >&2
echo "Perhaps try installing OpenSSL with your package manager first?" >&2
exit 1
fi
# This is your UConnect/OWA password or other LDAP credentials
echo -n " Input OWA/LDAP authentication password (not stored) > "
stty -echo # source: http://kwatog.com/blog/read-input-without-echo-in-bash/
read PASS
stty echo
SEARCH_CMD="ldapsearch -LLL -b $BASE_DN -D $BIND_DN -w $PASS -H $LDAP_URI"
#SEARCH_CMD="ldapsearch -LLL -b $BASE_DN -D $BIND_DN -W -H $LDAP_URI"
# If one command-line argument isn't given, or the given argument isn't a
# readable file:
echo
if [ $# -ne 1 -o ! -f "$1" ]; then
echo "ACK! Could not read file with list of names. Bailing out." >&2
exit 1
fi
# Check to make sure the LDAP server is up (ldapsearch returns 255 if not).
# Have to test (and throw away results) once because all the other results
# we want to capture in a variable.
echo
echo -n "Testing LDAP server connection... "
eval "$SEARCH_CMD -z 1 \"(sn=smith)\"" >/dev/null 2>&1
if [ $? -gt 0 ]; then
echo "FAILED."; echo
echo "Problem connecting to LDAP server at $LDAP_URI." >&2
echo "Check your credentials and/or DavMail error log. Bailing out." >&2
exit 1
fi # otherwise:
echo "done."
# Read "Last, First" from file and save as $lname, $fname.
# IFS = "Input Field Separator"; see bash(1)
while IFS=", " read lname fname; do
EMAIL=$( eval "$SEARCH_CMD \"(cn=$lname, $fname*)\" mail" | \
sed -n 's/mail: \(.*\)/\1/p' )
#echo "Return value: $?"
if [ -z "$EMAIL" ]; then
EMAIL="not found"
fi
echo $fname $lname \<$EMAIL\>
done < "$1" # read from the file specified as the first command-line argument
# ldap-query-last-first.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment