生产实践:
用于openldap一键安装(centos6.6测试通过),批量导入删除用户,需先将slapd.conf配置文件配置好,放置家目录下即可
学习技巧:
函数,while循环
脚本内容:
#!/bin/bash
############################################################
# $Name: install_ldap.sh
# $Version: v1.0
# $Function: For install openldap
# $Author: Zhihe An
# $Copyright (c) https://chegva.com
# $Create Date: 2017-12-13
############################################################
#centos6.6 x86_64 openldap-2.4.40-16.el6.x86_64
. /etc/init.d/functions
install() {
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \
&& wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo \
&& yum install openldap openldap-servers openldap-devel openldap-clients migrationtools -y \
&& cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG \
&& chown ldap:ldap /var/lib/ldap/DB_CONFIG && chmod 700 /var/lib/ldap \
&& cp ~/slapd.conf /etc/openldap/slapd.conf \
&& echo -e "local4.* /var/log/ldap.log" >> /etc/rsyslog.conf \
&& touch /var/log/ldap.log && chown ldap:ldap /var/log/ldap.log && /etc/init.d/rsyslog restart \
&& rm -rf /etc/openldap/slapd.d/* && chown -R ldap:ldap /etc/openldap/ \
&& /etc/init.d/slapd restart && chown -R ldap:ldap /var/lib/ldap/ \
&& slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/ \
&& chown -R ldap:ldap /etc/openldap/ && /etc/init.d/slapd restart \
&& yum -y install httpd php php-ldap php-gd php-mbstring php-pear php-bcmath php-xml \
&& yum -y install epel-release && yum --enablerepo=epel -y install phpldapadmin
}
ldapadmin() {
echo "<meta http-equiv=\"refresh\" content=\"0; url=/ldapadmin\">" > /var/www/html/index.html
#sed -i 's%Listen 80%Listen 8080%g' /etc/httpd/conf/httpd.conf
sed -i '397s/^[/]*//g' /etc/phpldapadmin/config.php
sed -n '397p' /etc/phpldapadmin/config.php
sed -i '398s/^/\/\//' /etc/phpldapadmin/config.php
sed -n '398p' /etc/phpldapadmin/config.php
cat >/etc/httpd/conf.d/phpldapadmin.conf << EOF
#
# Web-based tool for managing LDAP servers
#
Alias /phpldapadmin /usr/share/phpldapadmin/htdocs
Alias /ldapadmin /usr/share/phpldapadmin/htdocs
<Directory /usr/share/phpldapadmin/htdocs>
<IfModule mod_authz_core.c>
# Apache 2.4
Require local
#Require ip 10.112.136.145
Require all granted
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Allow from all
Allow from ::1
</IfModule>
</Directory>
EOF
}
startldap() {
/etc/init.d/slapd restart && /etc/init.d/httpd restart
}
main() {
action "openldap正在安装中..." /bin/true
install >/dev/null 2>&1
action "openldap安装完成..." /bin/true
action "ldapadmin正在安装中..." /bin/true
ldapadmin >/dev/null 2>&1
action "ldapadmin安装完成..." /bin/true
startldap >/dev/null 2>&1
action "openldap & httpd启动成功..." /bin/true
netstat -tunpl |egrep "slapd|httpd"
}
main批量添加用户:
ldapadd -x -D "cn=admin,dc=chegva,dc=com" -W -f user.ldif
#!/bin/bash
#user -> anzhihe123 fbi 110
while read line
do
a=($line)
pass=$(slappasswd -h {SSHA} -s ${a[0]})
cat >> ~/user.ldif << EOF
dn: cn=${a[0]},ou=fbi,dc=chegva,dc=com
changetype: add
objectClass: inetOrgPerson
description: ${a[1]}
cn: ${a[0]}
sn: ${a[0]}
o: ${a[1]}
userPassword: ${pass}
mail: ${a[0]}@chegva.com
mobile: ${a[2]}
EOF
done < user
#####清除数据重建
service slapd stop
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
chown -R ldap.ldap /etc/openldap/slapd.d
service slapd restart
chkconfig slapd on
netstat -tunpl|grep "slapd"
