使用python

时间:2017-10-22 04:47:56

标签: python scripting ldap

我正在尝试编写一个python脚本来验证特定域中是否存在用户名。

To query :
           username -- > anandabhis
           domain name --> example.com
Output : Successfully verified .

为此,我使用python-ldap模块连接到LDAP服务器。但即使在阅读了大量文件后我也无法继续前进。

import ldap
def test_login(self):
        domain = 'EXAMPLE'
        server = 'ldap-001.example.com'
        admin_username = 'admin'
        admin_password = 'secret-password'
        connection = ldap.initialize('ldap://{0}'.format(server))
        connection.protocol_version = 3
        connection.set_option(ldap.OPT_REFERRALS, 0)
        connection.simple_bind_s('{0}\{1}'.format(domain, admin_username), admin_password)
        search_username = 'anandabhis'

1 个答案:

答案 0 :(得分:0)

只需搜索用户的sAMAccountName即可获取用户的属性。

user_filter = '(sAMAccountName={})'.format(search_username)
base_dn = 'DC={},DC=com'.format(domain)
result = connection.search_s(base_dn, ldap.SCOPE_SUBTREE, user_filter)
print result