分类:
2005-06-06 14:27:58
从LDAP获取所有用户的用户信息:
#!/bin/sh
#name get_user_info.sh
BASEDN="uid=admin, ou=Administrators, ou=TopologyManagement, o=NetscapeRoot"
PASSWD="YOU PASSWORD"
ldapsearch -LLL -x -h MAIL_SERVER_IP -D "$BASEDN" -w "$PASSWD" -b "o=test.com" mail uid mailquota userpassword cn
输出结果:
dn: uid=user,o=test.com
mail: user@mail.test.com
uid: user
mailquota: 104857600
userpassword:: xxxxxxxx
cn: Mail User
dn: uid=user01,o=test.com
mail: user01@mail.test.com
uid: user01
userpassword:: xxxxxxxx
cn: Mail User01
使用ldapsearch输出的password是经过base64编码的,所以要解码。
执行./get_user_info.sh > all_users输出到all_users文件中。
#!/bin/sh
# name add_user_to_new_server.sh
# read file from XXXX then add to New mailserver
#dn: uid=user,o=test.com
#mail: user@mail.test.com
#uid: user
#mailquota: 104857600
#userpassword:: xxxxxxxx
#cn: Mail User
#
#dn: uid=user01,o=test.com
#mail: user01@mail.test.com
#uid: user01
#userpassword:: xxxxxxxx
#cn: Mail User01
TMP_MAIL=""
TMP_UID=""
TMP_MAILQUOTA=""
TMP_USERPASSWORD=""
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
cat all_users | while read keys values
do
# if TMP_MAIL not empty, add user
if [ "ELM$keys" = "ELM" ] ; then
if [ "ELM$TMP_MAIL" != "ELM" -a "ELM$TMP_UID" != "ELM" ] ; then
if [ "ELM$TMP_MAILQUOTA" != "ELM" ] ; then
echo $TMP_MAIL $TMP_UID $TMP_MAILQUOTA $TMP_USERPASSWORD
#增加用户执行的命令
else
echo $TMP_MAIL $TMP_UID $TMP_USERPASSWORD
#增加用户执行的命令
fi
fi
fi
# if keys eq dn: clean all values
if [ "ELM$keys" = "ELMdn:" ] ; then
TMP_MAIL=""
TMP_UID=""
TMP_MAILQUOTA=""
TMP_USERPASSWORD=""
fi
# check values
if [ "ELM$keys" = "ELMmail:" ] ; then
TMP_MAIL=$values
fi
if [ "ELM$keys" = "ELMuid:" ] ; then
TMP_UID=$values
fi
if [ "ELM$keys" = "ELMmailquota:" ] ; then
#限额转换成以M为单位
TMP_MAILQUOTA=`expr $values / 1048576`
fi
if [ "ELM$keys" = "ELMuserpassword::" ] ; then
TMP_USERPASSWORD=`openssl base64 -d <<__ELM__
$values
__ELM__`
fi
done
在刚才生成的all_users文件所在目录执行./add_user_to_new_server.sh就OK了
如果上面脚本有问题,在 $ 后面的变量加上 { } ,不知道为什么写上就显示不了