由于公司的审计邮箱在windows下运行一直不太稳定,为此我将邮箱移植到linux Iredmail下(具体的Iredmail安装方法我就不过多介绍文档很多),我们的用户名和密码都是如下形式

test1:123456
test2:234566
但是我们有将近2万的用户需要导入,于是问了一下Iredmail的管理员批量导入用户的问题,得知只可以批量创建用户但是没有批量导入用户名和密码这个功能。于是就找到了批量创建用户的脚本,做了修改,可以成功实现想要的功能,具体脚本如下:
这里说明一下这个脚本的原作者是Zhang Huangbin,我是做了二次功能修改实现的。(这脚本执行过后就生成了一个output.sql文件里面是所有用户的标准SQL语句,你只需执行导入到相应的库里就可以了)
#!/usr/bin/env bash

#Auther:YooMa

STORAGE_BASE_DIRECTORY=”/var/vmail/vmail1″

# Password setting.

# Note: password will be crypted in MD5.

DEFAULT_PASSWD=’88888888′

USE_DEFAULT_PASSWD=’NO’ # If set to ‘NO’, password is the same as username.

# Default mail quota.

DEFAULT_QUOTA=’100′ # 100 -> 100M

# ————– You may not need to change variables below ——————-

# Mailbox format: mbox, Maildir.

MAILBOX_FORMAT=’Maildir’

MAILDIR_STYLE=’hashed’ # hashed, normal.

# Time stamp, will be appended in maildir.

DATE=”$(date +%Y.%m.%d.%H.%M.%S)”

STORAGE_BASE=”$(dirname ${STORAGE_BASE_DIRECTORY})”

STORAGE_NODE=”$(basename ${STORAGE_BASE_DIRECTORY})”

# Path to SQL template file.

SQL=”output.sql”

echo ” > ${SQL}

# Cyrpt the password.

generate_sql()

{

        # Get domain name.

        DOMAIN=”test.net.cn” #要进行哪个邮箱域的导入

        UserList=/root/iRedMail-0.8.3/tools/user10.txt #这里是存放用户和密码的用户列表

        cat $UserList | while read line

        do

                arr=($line)

                username=${arr[0]}

                pswd=${arr[1]}

                mail=”${username}@${DOMAIN}”

                export CRYPT_PASSWD=”$(openssl passwd -1 ${pswd})”

                # Different maildir style: hashed, normal.

                if [ X”${MAILDIR_STYLE}” == X”hashed” ]; then

                        length=”$(echo ${username} | wc -L)”

                        str1=”$(echo ${username} | cut -c1)”

                        str2=”$(echo ${username} | cut -c2)”

                        str3=”$(echo ${username} | cut -c3)”

                        if [ X”${length}” == X”1″ ]; then

                                str2=”${str1}”

                                str3=”${str1}”

                        elif [ X”${length}” == X”2″ ]; then

                                str3=”${str2}”

                        else

                                :

                        fi

                        # Use mbox, will be changed later.

                        maildir=”${DOMAIN}/${str1}/${str2}/${str3}/${username}-${DATE}”

                else

                        # Use mbox, will be changed later.

                        maildir=”${DOMAIN}/${username}-${DATE}”

                fi

                # Different maildir format: maildir, mbox.

                if [ X”${MAILBOX_FORMAT}” == X”Maildir” ]; then

                        # Append slash to make it ‘maildir’ format.

                        maildir=”${maildir}/”

                else

                        # It’s already mbox format.

                        :

                fi

                cat >> ${SQL} <<EOF

INSERT INTO mailbox (username, password, name, storagebasedirectory,storagenode, maildir, quota, domain, active, local_part, created)

        VALUES (‘${mail}’, ‘${CRYPT_PASSWD}’, ‘${username}’, ‘${STORAGE_BASE}’,’${STORAGE_NODE}’, ‘${maildir}’, ‘${DEFAULT_QUOTA}’, ‘${DOMAIN}’, ‘1’,’${username}’, NOW());

INSERT INTO alias (address, goto, domain, created, active) VALUES (‘${mail}’, ‘${mail}’,’${DOMAIN}

generate_sql