Attachment 'my_search.py'
Download 1 #!/usr/bin/env python
2
3 import ldap
4
5 def main():
6 # It looks something like this:
7 server = "localhost"
8 who = "cn=Manager,dc=example,dc=com"
9 cred = "secret"
10
11
12 #Now we need to make a keyword set to what we want our search string to be. I use my first name for this sample program:
13 keyword = "Donald Duck"
14
15 #Next, we need to bind to the LDAP server. Doing so creates an object named "l" that is then used throughout the program.
16 try:
17 los = ldap.open(server)
18 los.simple_bind_s(who, cred)
19 print "Successfully bound to server.\n"
20
21
22 #We're now ready to query the server. We also now catch any possible errors if there is a problem authenticating:
23 print "Searching..\n"
24 my_search(los, keyword)
25 except ldap.LDAPError, error_message:
26 print "Couldn't Connect. %s " % error_message
27
28
29 def my_search(los, keyword):
30
31 #In a moment we will be calling python-ldap's built-in search method on our l object. Four variables--base, scope, filter and retrieve_attributes--are the parameters of that search method. Base is used for the DN (distinguished name) of the entry where the search should start. You can leave it blank for this example:
32 base = "ou=People,dc=example,dc=com"
33
34 #For scope we use SCOPE_SUBTREE to search the object and all its descendants:
35 scope = ldap.SCOPE_SUBTREE
36
37 #Our search filter consists of a cn (common name) and our keyword. Putting asterisks around our keyword (ryan) will match anything with the string ryan, such as Bryant.
38 filter_s = "cn=" + keyword
39
40
41 #The last argument we pass to the search method is used to return all the attributes of each entry:
42 retrieve_attributes = None
43
44 #Now, let's setup a few more variables, including a counter to keep track of the number of results returned:
45 count = 0
46
47 #a list to append the results to:
48 result_set = []
49
50 #and a variable to specify the length, in seconds, we're willing to wait for a response from the server:
51 timeout = 0
52
53 #Now we can begin our search by calling python-ldap's search method on our l object:
54 try:
55 result_id = los.search(base, scope, filter_s, retrieve_attributes)
56
57 #Store any results in the result_set list
58 while 1:
59 result_type, result_data = los.result(result_id, timeout)
60 if (result_data == []):
61 break
62 else:
63 if result_type == ldap.RES_SEARCH_ENTRY:
64 result_set.append(result_data)
65
66
67 #If we were to print result_set now, it might look like a big list of tuples and dicts. Instead, step through it and select only the data we want to see
68 if len(result_set) == 0:
69 print "No Results."
70 return
71 for i in range(len(result_set)):
72 for entry in result_set[i]:
73 print entry
74 try:
75 name = entry[1]['cn'][0]
76 email = entry[1]['mail'][0]
77 phone = entry[1]['telephonenumber'][0]
78 desc = entry[1]['description'][0]
79 count = count + 1
80
81
82 #Display the data, if any was found, in the following format:
83 #1. Name: Description: E-mail: Phone: 2. Name: Description: E-mail: Phone: etc..
84 print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\
85 (count, name, desc, email, phone)
86 except:
87 pass
88
89 except ldap.LDAPError, error_message:
90 print error_message
91
92
93 #Running the Program
94 #You probably are anxious now to run the program and see it in action. But first, you need to append the following code to call the main() function. It should go at the end of your script before you run it.
95 if __name__ == '__main__':
96 main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.