LDAP批量添加php页面,包含邮件发送

页面index.html

<html lang="en">
<head>
  <!-- <meta charset="UTF-8"> -->
  <title>ldap添加用户</title>
</head>

<form action="admin_f.php" method="post"  name="commentform">
  <table  border="0" cellpadding="0" cellspacing="0">
   <tr><td width="500" height="30">请输入信息 (帐号,密码):<br/>
<textarea style="width:400px;height:300px;text-align:left;" type="text" name="txt_info" size="12" placeholder="请在这里输入信息,例如 group,user,[email protected]"></textarea><br/>
      <input type="submit" name="submit" value="登录">
     </td>
   </tr>
  </table>
</form>

</body>
</html>

处理页面

admin_f.php

<?php
$user_pd="2018";
$dc='ou=people,dc=ldap,dc=com';
$object="inetOrgPerson";
$hostname="192.168.5.121:389";
$admin='cn=admin,dc=ldap,dc=com';
$admin_pw="admin2018";
$ds=ldap_connect($hostname) or die("Could not connect to LDAP server.");

        if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
                   $echo_info.= '\\n'."使用 LDAPv3  protocol version";
        }else{
                   echo '\\n'."Failed to set protocol version to 3".'\\n';
        }
        $r=ldap_bind($ds,$admin,$admin_pw);
        if($r){
                $echo_info.='\\n'."连接Ldap服务器成功";
        }else{
                echo "<br/>连接Ldap服务器失败,请稍候再尝试,如失败,请联系admin";
        }

if ($_REQUEST["txt_info"]){
	$str=explode("\n",$_REQUEST["txt_info"]);
	foreach ($str as $value){
		$string= trimall($value);
		if( $string == null ){
                continue;
       		 }
		$str_info=explode(",",$string);
		if($str_info[0] != "people"){
			$user_dc='ou='.$str_info[0].",".$dc;
		}else{
			$user_dc=$dc;
		}
		$username=$str_info[1];
	        $Email=$str_info[2];
		$dc_address='cn='.$username.",".$user_dc;
		$info["cn"]=$username;
		$info["sn"]=$username;
		$info["userpassword"]=$user_pd;
		$info["mail"]=$Email;
		$info["objectclass"]=$object;
		
		$result=ldap_add($ds,$dc_address, $info);
                if($result){
                        $echo_info.='\\n'."添加".$username."用户成功";
			$locale='en_US.UTF-8';
			setlocale(LC_ALL,$locale);
			putenv('LC_ALL='.$locale);
			system( "sh fmail.sh {$Email} {$username} ");
			$echo_info.='\\n'."邮件".$Email."发送成功".'\\n';
                }else{
                        echo "<br/>error:faile,添加信息为".var_dump($ds);
                }
	}
ldap_unbind($ds);
ldap_close($ds);
echo "<script>alert('{$echo_info}点击返回上一页!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
}else{
	echo "<script>alert('输入内容不能为空!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
}

function trimall($str){
    $qian=array(" "," ","\t","\\n","\r");
    return str_replace($qian, '', $str);
}

?>

邮件发送页面 fmail.sh (注:自助修改密码程序已在前文介绍过https://blog.csdn.net/zhangxueleishamo/article/details/82179902)

#!/bin/bash
mail_zt="ldap帐号已开通"

mail -s $mail_zt $1 << EOF

ldap帐号已开通,用户名: $2,密码默认2018

自助修改密码连接 http://passwd.com.cn/

目前可登陆wiki,svn,git其他功能待续...

wiki地址: http://wiki.com.cn/
svn地址 : http://svn.com.cn/
git地址 :http://git.com.cn/

EOF

mail配置

yum -y install mailx
vim /etc/mail.rc
set [email protected]

set smtp=mail.com.cn

set [email protected]

set smtp-auth-password=1234567890

set smtp-auth=login

猜你喜欢

转载自blog.csdn.net/zhangxueleishamo/article/details/82453879