欢迎各位兄弟 发布技术文章
这里的技术是共享的
要通过 PHP LDAP 获取名为 MM-11111111
的计算机信息,你需要以下步骤:
php<?php // LDAP服务器连接信息 $ldapServer = "ldap://your.domain.controller"; // 替换为你的域控制器地址 $ldapUser = "cn=admin,dc=example,dc=com"; // 替换为有查询权限的账户 $ldapPass = "password"; // 替换为密码 // 连接到LDAP服务器 $ldapConn = ldap_connect($ldapServer); if (!$ldapConn) { die("无法连接到LDAP服务器"); } // 设置LDAP选项 ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0); // 绑定到LDAP服务器 $ldapBind = ldap_bind($ldapConn, $ldapUser, $ldapPass); if (!$ldapBind) { die("LDAP绑定失败: " . ldap_error($ldapConn)); } // 搜索计算机对象 $searchBase = "ou=Computers,dc=example,dc=com"; // 替换为你的计算机OU路径 $filter = "(cn=MM-11111111)"; // 计算机名过滤条件 $attributes = ["cn", "description", "operatingSystem", "lastLogon", "whenCreated"]; // 要获取的属性 $search = ldap_search($ldapConn, $searchBase, $filter, $attributes); $entries = ldap_get_entries($ldapConn, $search); if ($entries["count"] > 0) { // 输出计算机信息 echo "<h2>找到计算机: {$entries[0]['cn'][0]}</h2>"; echo "<pre>" . print_r($entries[0], true) . "</pre>"; } else { echo "未找到计算机 MM-11111111 "; } // 关闭连接 ldap_close($ldapConn); ?>
连接参数:
替换 ldap://your.domain.controller
为你的域控制器地址
替换 cn=admin,dc=example,dc=com
为有查询权限的账户DN
替换 ou=Computers,dc=example,dc=com
为你组织中的计算机OU路径
计算机属性:
cn
: 计算机名
description
: 描述信息
operatingSystem
: 操作系统
lastLogon
: 最后登录时间
whenCreated
: 创建时间
时间格式转换: LDAP中的时间通常是Windows文件时间格式,你可能需要转换:
phpfunction ldapTimeToDateTime($ldapTime) { $winSecs = (int)($ldapTime / 10000000); // 转换为秒 $unixTimestamp = $winSecs - 11644473600; // 从1601年到1970年的秒数 return date('Y-m-d H:i:s', $unixTimestamp); }
确保PHP安装了LDAP扩展(php-ldap)
根据你的AD结构调整搜索基(Search Base)
可能需要调整LDAP协议版本(通常使用版本3)
如果使用SSL连接,确保端口正确(通常是636)
如果没有找到计算机,请检查:
计算机名是否正确
搜索基是否包含该计算机
查询账户是否有足够的权限