以下是 Golang 中查询域用户列表的示例代码:

package main

import (
    "fmt"
    "gopkg.in/ldap.v3"
    "log"
)

func main() {
    // 设置LDAP服务器信息
    ldapServer := 'ldap://ldap.example.com:389'
    ldapBindDN := 'CN=Administrator,CN=Users,DC=example,DC=com'
    ldapBindPassword := 'password'
    ldapSearchBase := 'OU=Users,DC=example,DC=com'

    // 连接LDAP服务器
    ldapConn, err := ldap.Dial('tcp', ldapServer)
    if err != nil {
        log.Fatal(err)
    }
    defer ldapConn.Close()

    // 绑定LDAP管理员账号
    err = ldapConn.Bind(ldapBindDN, ldapBindPassword)
    if err != nil {
        log.Fatal(err)
    }

    // 设置LDAP搜索过滤条件,获取所有用户
    searchRequest := ldap.NewSearchRequest(
        ldapSearchBase,
        ldap.ScopeWholeSubtree,
        ldap.NeverDerefAliases,
        0,
        0,
        false,
        '(objectClass=user)',
        []string{'sAMAccountName'},
        nil,
    )

    // 执行LDAP搜索
    searchResult, err := ldapConn.Search(searchRequest)
    if err != nil {
        log.Fatal(err)
    }

    // 输出搜索结果
    for _, entry := range searchResult.Entries {
        fmt.Println(entry.GetAttributeValue('sAMAccountName'))
    }
}

在上述示例代码中,我们使用了gopkg.in/ldap.v3库连接LDAP服务器,并执行了一个搜索操作来获取所有用户的sAMAccountName属性值。您需要根据您的实际情况修改LDAP服务器信息、管理员账号信息和搜索过滤条件。

Golang LDAP 查询域用户列表示例代码

原文地址: https://www.cveoy.top/t/topic/koBV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录