Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Created December 2, 2011 16:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save metalrufflez/1423935 to your computer and use it in GitHub Desktop.
Save metalrufflez/1423935 to your computer and use it in GitHub Desktop.
Mass create zimbra accounts
#/bin/bash
# zmcreateusers
# Mass create zimbra accounts
#
# AUTHORS: Caio Corrêa, Thompson Alves
# LAST UPDATED: 2011/12/02
#
# Version 1.2: Added the report at the ending
# Version 1.1: Added security check of alias and username
# Version 1.0: First release, creates accounts and aliases
# ========================= CAVEAT ===============================
# The file you must pass as a parameter MUST have this format
# for each account you wish to create
#
# username:Display Name:Given Name:Surname:PASSWORD:alias
#
# Note that the ALIAS is optional
DOMAIN="yourdomain.com"
# Vars used for the end report
SUCCESS="0"
ERROR="0"
RESUMEFILE="/tmp/zmcreateresume"
ALIASRESUMEFILE="/tmp/zmaliascreateresume"
# Getopts to get the debug option
while getopts ":d" opt; do
case $opt in
d) DEBUG="echo";;
\?) echo "Invalid option -$OPTARG"
exit 1;;
esac
done
shift $((OPTIND-1))
# Check if the user passed one and only one parameter
if [ "$#" -ne 1 ]; then
echo "USAGE: $0 [-d] <file>"
exit 1
fi
FILE=$1
# Check if the passed parameter is a regular file
if [ ! -f "$FILE" ]; then
echo "\"$FILE\" does not exist or is not a regular file"
exit 1
fi
# Loop each line, parse it and create each user
while read line; do
USERNAME=$(echo $line | cut -d":" -f1)
DISPLAYNAME=$(echo $line | cut -d":" -f2)
GIVENNAME=$(echo $line | cut -d":" -f3)
SURNAME=$(echo $line | cut -d":" -f4)
PASSWD=$(echo $line | cut -d":" -f5)
ALIAS=$(echo $line | cut -d":" -f6 | tr [A-Z] [a-z])
echo $DISPLAYNAME
# Check if $USERNAME already exists, if so, skip
CHECKNAME="$(zmprov ga $USERNAME@$DOMAIN displayName 2> /dev/null | grep Name | cut -d" " -f2-)"
if [ ! -z "$CHECKNAME" ]; then
echo "\"$USERNAME\" - Account Already Exists: \"$CHECKNAME\"" | tee -a $RESUMEFILE
ERROR=$((ERROR+1))
continue
fi
$DEBUG zmprov ca $USERNAME@$DOMAIN $PASSWD displayName "$DISPLAYNAME" givenName "$GIVENNAME" sn "$SURNAME" zimbraAccountStatus active
# Check if the user was correctly created, if not, skip
if [ "$?" -ne 0 ]; then
echo "$USERNAME - Error Creating Account" >> $RESUMEFILE
ERROR=$((ERROR+1))
continue
else
SUCCESS=$((SUCCESS+1))
fi
# Check if the there is an ALIAS in the file, if so, add alias
if [ ! -z "$ALIAS" ]; then
# Check if the alias already exists
CHECKALIAS="$(zmprov ga $ALIAS@$DOMAIN displayName 2> /dev/null | grep Name | cut -d" " -f2-)"
if [ ! -z "$CHECKALIAS" ]; then
echo "\"$ALIAS\" ($USERNAME) - Alias Already Exists: \"$CHECKALIAS\"" | tee -a $ALIASRESUMEFILE
else
$DEBUG zmprov aaa $USERNAME@$DOMAIN $ALIAS@$DOMAIN
fi
fi
done < $FILE
echo
echo "============================================="
echo "Accounts Succesfully Created: $SUCCESS"
echo "Accounts Not Created: $ERROR"
echo "============================================="
if [ "$ERROR" -ne 0 ]; then
echo "Accounts with error:"
echo
cat $RESUMEFILE
echo
rm -f $RESUMEFILE
fi
if [ -f $ALIASRESUMEFILE ]; then
echo "============================================="
echo "Alias with error:"
echo
cat $ALIASRESUMEFILE
echo
rm -f $ALIASRESUMEFILE
fi
@oquidave
Copy link

oquidave commented May 3, 2016

Hey thanks for this. I just forked it and modified it to run my own migration needs. If you're new to ZImbra though, it should be noted that you've to first login as zimbra after being root "root@host#su - zimbra", make the file executable with chmod +x file.sh before running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment