python登录windows AD域并查看用户信息

# -*- coding: UTF-8 -*-

#
# python ldap doc
# http://www.python-ldap.org/en/latest/reference/ldap.html
#

import ldap, json

def ldap_login(ldap_path, user, domain, passwd):
    try:
        c = ldap.initialize(ldap_path)
        c.protocol_version = ldap.VERSION3
        d = c.simple_bind("%s@%s"%(user, domain), passwd)
    except ldap.INVALID_CREDENTIALS, e:
        c.unbind()
        c = d = None
    finally:
        return c, d

def user_info(c, user, base):
    try:
        scope  = ldap.SCOPE_SUBTREE
        filterstr = "(sAMAccountName=%s)"%user

        id = c.search(base, scope, filterstr, None)

        result_type, result_data = c.result(id, 1)
        return result_data
    except ldap.LDAPError, e:
        return None
    finally:
        pass

####

ldap_path = "ldap://192.168.22.129:389"
base = "DC=dev,DC=com"
user = "test"
domain = "dev.com";
passwd = "1qaz@WSX";

c, d = ldap_login(ldap_path, user, domain, passwd)

if c == None:
    print "login error!"
    exit(1)

print "user: %s login ok!"%user
info = user_info(c, user, base)
print json.dumps(info, ensure_ascii=False, indent=4)

c.unbind()
程序返回结果
user: test login ok!
[
    [
        "CN=test,CN=Users,DC=dev,DC=com", 
        {
            "primaryGroupID": [
                "513"
            ], 
            "logonCount": [
                "2"
            ], 
            "cn": [
                "test"
            ], 
            "countryCode": [
                "0"
            ], 
            "objectClass": [
                "top", 
                "person", 
                "organizationalPerson", 
                "user"
            ], 
            "userPrincipalName": [
                "test@dev.com"
            ], 
            "adminCount": [
                "1"
            ], 
            "lastLogonTimestamp": [
                "131911416435300980"
            ], 
            "instanceType": [
                "4"
            ], 
            "distinguishedName": [
                "CN=test,CN=Users,DC=dev,DC=com"
            ], 
            "dSCorePropagationData": [
                "20190105065609.0Z", 
                "16010101000000.0Z"
            ], 
            "objectSid": [
                "\u0001\u0005\u0000\u0000\u0000\u0000\u0000\u0005\u0015\u0000\u0000\u00000\u0004\u0000\u0000"
            ], 
            "whenCreated": [
                "20190105055908.0Z"
            ], 
            "uSNCreated": [
                "12765"
            ], 
            "badPasswordTime": [
                "0"
            ], 
            "pwdLastSet": [
                "131911415491078266"
            ], 
            "sAMAccountName": [
                "test"
            ], 
            "objectCategory": [
                "CN=Person,CN=Schema,CN=Configuration,DC=dev,DC=com"
            ], 
            "lastLogon": [
                "131911419521867631"
            ], 
            "objectGUID": [
                "g002+"
            ], 
            "whenChanged": [
                "20190105065609.0Z"
            ], 
            "badPwdCount": [
                "0"
            ], 
            "accountExpires": [
                "9223372036854775807"
            ], 
            "displayName": [
                "test"
            ], 
            "name": [
                "test"
            ], 
            "memberOf": [
                "CN=Administrators,CN=Builtin,DC=dev,DC=com"
            ], 
            "codePage": [
                "0"
            ], 
            "userAccountControl": [
                "66048"
            ], 
            "sAMAccountType": [
                "805306368"
            ], 
            "uSNChanged": [
                "12788"
            ], 
            "sn": [
                "test"
            ], 
            "lastLogoff": [
                "0"
            ]
        }
    ], 
    [
        null, 
        [
            "ldap://ForestDnsZones.dev.com/DC=ForestDnsZones,DC=dev,DC=com"
        ]
    ], 
    [
        null, 
        [
            "ldap://DomainDnsZones.dev.com/DC=DomainDnsZones,DC=dev,DC=com"
        ]
    ], 
    [
        null, 
        [
            "ldap://dev.com/CN=Configuration,DC=dev,DC=com"
        ]
    ]
]
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nowaits

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值