Learning Python Network Programming

(Sean Pound) #1

Interacting with Remote Systems


The LDAP search request generates a server response, which has been shown here:


When the LDAP server returns the search response, we can see the format of the
response. As shown in the preceding screenshot, it contains the result of the search
and the associated attributes.


Here is an example of searching a user from an LDAP server:


#!/usr/bin/env python
import ldap
import ldap.modlist as modlist

LDAP_URI = "ldap://10.0.2.15:389/"
BIND_TO = "dc=localdomain,dc=loc"
BASE_DN = 'ou=users,dc=localdomain,dc=loc'
SEARCH_FILTER = '(objectclass=person)'
SEARCH_FILTER = ['sn']

if __name__ == '__main__':
# Open a connection
l = ldap.initialize(LDAP_URI)
# bind to the server
l.simple_bind(BIND_TO)
result = l.search_s( BASE_DN, ldap.SCOPE_SUBTREE,
SEARCH_FILTER, SEARCH_FILTER )
print(result)
Free download pdf