import ldap server 10 10 249 def my_search keyword base OU Office user

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import ldap
server = "10.10.5.249"
def my_search(l, keyword):
base = "OU=Office users,DC=ADTEL,DC=mcomm,DC=ru"
scope = ldap.SCOPE_SUBTREE
filter = "sAMAccountName=" + keyword
retrieve_attributes = ['name', 'email']
count = 0
result_set = []
timeout = 0
try:
result_id = l.search(base, scope, filter, retrieve_attributes)
while 1:
result_type, result_data = l.result(result_id, timeout)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
if len(result_set) == 0:
print "No Results."
return
for i in range(len(result_set)):
for entry in result_set[i]:
try:
name = entry[1]['cn'][0]
email = entry[1]['mail'][0]
count = count + 1
print "%d.\nName: %s\nE-mail: %s\n" %\
(count, name, email,)
except:
pass
except ldap.LDAPError, error_message:
print error_message
try:
l = ldap.open(server)
res = l.bind('afedorov@ADTEL.ru', '', ldap.AUTH_SIMPLE)
print res
print "Successfully bound to server.\n"
print "Searching..\n"
my_search(l, 'afedorov')
l.unbind()
except ldap.LDAPError, error_message:
print "Couldn't Connect. %s " % error_message