Reputation: 283
I would like to get all PCs in the local network from ldap, so I tried (variations of) this:
import ldap3
from ldap3 import ALL_ATTRIBUTES, SUBTREE, ALL
import dns.resolver
import socket
def get_ldap_server():
domain_name = socket.getfqdn().lstrip( socket.gethostname() )
answers = dns.resolver.query( '_ldap._tcp'+domain_name, rdtype='srv' )
#for srv in answers:
return answers[0].target.to_text()[:-1]
srv_name = get_ldap_server()
print srv_name
server = ldap3.Server( srv_name, get_info=ALL )
with ldap3.Connection( server ) as c:
print "Bound", c.bound
c.search( search_base='dc='+', dc='.join(srv_name.split('.')[1:]),
search_filter='(objectCategory=computer)',
search_scope=SUBTREE,
attributes=ALL_ATTRIBUTES,
get_operational_attributes=True)
print(c.response)
But all I get is: LDAPOperationsErrorResult: LDAPOperationsErrorResult - 1 - operationsError - None - 000004DC: LdapErr: DSID-0C090748, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580 - searchResDone - None
Despite "Bound" being "True".
I'm using python 2.7. Any help would be greatly appreciated!
Upvotes: 1
Views: 1538
Reputation: 1588
You didn't provide any username or password in the connection object, so an anonymous bind is performed.
Try adding username=xxx and password=yyy to the Connection definition in the "with" statement.
Upvotes: 2