Learning Python Network Programming

(Sean Pound) #1

Interacting with Remote Systems


The preceding communication can be captured with the help of Wireshark. You
need to capture the packets on port 389. As shown in the following screenshot, the
LDAP client-server communication will be established after a bindRequest has
been successfully sent. It's not secure to communicate anonymously with the LDAP
server. For the sake of simplicity, in the following example the search has been done
without binding with any of the credentials.


The Python's third-party python-ldap package provides the necessary functionality
for interacting with an LDAP server. You can install this package with the help
of pip.


$ pip install python-ldap


To begin with, you will have to initialize the LDAP connection:


import ldap
ldap_client = ldap.initialize("ldap://10.0.2.15:389/")

Then the following code will show how a simple BIND operation can be performed:


ldap_client.simple_bind("dc=localdomain,dc=loc")

Then you can perform an ldap search. It requires you to specify the necessary
parameters, such as base DN, filter, and attributes. Here is an example of the syntax
that is required for searching for the users on an LDAP server:


ldap_client.search_s( base_dn, ldap.SCOPE_SUBTREE, filter, attrs )

Here is a complete example for finding user information by using the LDAP protocol:


import ldap

# Open a connection
ldap_client = ldap.initialize("ldap://10.0.2.15:389/")
Free download pdf