Ahmed Wagdi
Ahmed Wagdi

Reputation: 4391

How to get child_object in python-ldap

I'm totally new in using LDAP with python, am trying to perform a query on the LDAP tree that is shown in the image.

LDAP Tree

Using the package python-ldap . as you see, the tree is like this :

ou=Organization >> ou=Company >> ou=Brasil >> Then after the country, there are many cities, I managed to reach this city .. but there are some other cities that have some kind of (Sub-Cities) under it as you see in ou=Sao Paulo, it has 3 sub-cities after it.

What I need to do is :

1- Get to the child cities after the main cities. 2- get to be able to loop over every CN=Gateway-**** inside every city. 3- Finally need to access the inner attributes for every CN and get the value of it.

The code I used in views.py is :


def index(request):
    cities_list = []
    gateways_list = []
    con = ldap.initialize('ldap://The_URI_For_IDAP/(|(OU=Brasil)(OU=Company)(OU=Organization))')
    some_dn = 'dc=*****,dc=com'
    query = "ou=*"
    result = con.search_s(some_dn, ldap.SCOPE_SUBTREE, query)
    for item in result:
        cities_list.append(city_name)
        print(item)
    context = {
        'cities_list': cities_list,
    }
    return render(request, 'index.html', context)

in the cities_list i get a list of every Parent city ou after ou=Brasil but i can't get to the inner Ou and also can't reach the CN part.

Upvotes: 0

Views: 761

Answers (1)

EricLavault
EricLavault

Reputation: 16055

If you want to grab every entry under ou=Organization >> ou=Company >> ou=Brasil, you need to set the base dn accordingly and use objectclass=* as filter :

con = ldap.initialize('ldap://domain.com')
base_dn = 'ou=Brasil,ou=Company,ou=Organization,dc=domain,dc=com'
filter = "(objectClass=*)"
result = con.search_s(base_dn, ldap.SCOPE_SUBTREE, filter)

Upvotes: 1

Related Questions