python ldap3 获取域控制器列表

huangapple go评论49阅读模式
英文:

python ldap3 get domain controller list

问题

I need some help with a script, I am trying to get a list of all the domain controllers for a domain. I am using python ldap3 and I am getting errors trying to connect. See below, any suggestions will be appreciated.

Code

#!/usr/bin/python3

from ldap3 import Server, Connection

# LDAP server configuration
ldap_server = 'ldap://company.com'
ldap_user = 'username'
ldap_password = 'mypassword'

# Connect to the LDAP server
server = Server(ldap_server, get_info=Server.info)
conn = Connection(server, ldap_user, ldap_password, auto_bind=True)

# Search for domain controllers
conn.search(search_base='CN=Domain Controllers,DC=company,DC=com',
            search_filter='(objectClass=computer)',
            attributes=['name'])

# Print the list of domain controllers
print("Domain Controllers:")
for entry in conn.entries:
    print(entry.name)

Output

Traceback (most recent call last):
  File "./query-dc-list4.py", line 13, in <module>
    conn = Connection(server, ldap_user, ldap_password, auto_bind=True)
  File "/usr/lib/python3.6/site-packages/ldap3/core/connection.py", line 356, in __init__
    self._do_auto_bind()
  File "/usr/lib/python3.6/site-packages/ldap3/core/connection.py", line 405, in _do_auto_bind
    raise LDAPBindError(error)
英文:

I need some help with a script, I am trying to get a list of all the domain controllers for a domain. I am using python ldap3 and I am getting errors trying to connect. See below, any suggestions will be appreciated.

Code

#!/usr/bin/python3


from ldap3 import Server, Connection

# LDAP server configuration
ldap_server = &#39;ldap://company.com&#39;
ldap_user = &#39;username&#39;
ldap_password = &#39;mypassword&#39;

# Connect to the LDAP server
server = Server(ldap_server, get_info=Server.info)
conn = Connection(server, ldap_user, ldap_password, auto_bind=True)

# Search for domain controllers
conn.search(search_base=&#39;CN=Domain Controllers,DC=company,DC=com&#39;,
            search_filter=&#39;(objectClass=computer)&#39;,
            attributes=[&#39;name&#39;])

# Print the list of domain controllers
print(&quot;Domain Controllers:&quot;)
for entry in conn.entries:
    print(entry.name)

Output

Traceback (most recent call last):
  File &quot;./query-dc-list4.py&quot;, line 13, in &lt;module&gt;
    conn = Connection(server, ldap_user, ldap_password, auto_bind=True)
  File &quot;/usr/lib/python3.6/site-packages/ldap3/core/connection.py&quot;, line 356, in __init__
    self._do_auto_bind()
  File &quot;/usr/lib/python3.6/site-packages/ldap3/core/connection.py&quot;, line 405, in _do_auto_bind
    raise LDAPBindError(error)

答案1

得分: 0

我刚刚意识到我的基本搜索是不正确的。应该是以下内容:

conn.search(search_base='OU=Domain Controllers,DC=company,DC=com',
            search_filter='(objectClass=computer)',
            attributes=['name'])
英文:

I just realized that my base search was incorrect. it should be the following

conn.search(search_base=&#39;OU=Domain Controllers,DC=company,DC=com&#39;,
            search_filter=&#39;(objectClass=computer)&#39;,
            attributes=[&#39;name&#39;])

huangapple
  • 本文由 发表于 2023年6月15日 13:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定