ldapsearch to find DN for a user


 
Thread Tools Search this Thread
Operating Systems AIX ldapsearch to find DN for a user
# 1  
Old 07-26-2010
Data ldapsearch to find DN for a user

How can I do a ldapsearch to find a DN for a user when I know the exact cn for that user out of active directory.

I have tried several different commands (hundreds) but need the -b with the full dn to perform the search using ldapsearch from AIX. I am trying to find the OU for a user and the sAMAccountName. Our user are in several different OU's.

example that works:
Code:
ldapsearch -h something.acorp.com -p 389 -D "cn=${USER},ou=RealOU,DC=something,DC=acorp,DC=com" -w "${PW}" -s sub -b "cn=${SEARCH_USER},ou=UsersRealOU,DC
=something,DC=acorp,DC=com" "cn=*"

example that does not work (one of many):
Code:
ldapsearch -h something.acorp.com -p 389 -D "cn=${USER},ou=RealOU,DC=something,DC=acorp,DC=com" -w "${PW}" -s sub "cn=${SEARCH_USER}"


Last edited by pludi; 07-26-2010 at 04:47 PM.. Reason: code tags, please...
# 2  
Old 07-26-2010
How about something like this... it searches from the TOP of the
tree (i.e. dc=something,dc=acorp,dc=com) and searches for a
"user" with a same samaccountname equal to "$search_name":

Code:
ldapsearch \
           -H ldap://something.acorp.com \
           -b dc=something,dc=acorp,dc=com \
           -w my_passwd                             \
           -D  $MY_DISTINGUISHED_NAME   \
           '(&(objectclass=user)(samaccountname=$search_name))'\
            DN

# 3  
Old 07-27-2010
maybe I am missing something. I do not want to put in a DN in the search portion, I am looking for the DN Smilie. Or I am at least looking for OU portiton that is part of the DN, and that part is different for each user.

Also running your query in AIX I get this error:
Code:
scope is required for a null based search.
Sends a search request to an LDAP server.
usage:
    ldapsearch [-b basedn] [options] filter [attributes...]
where:
        basedn:     base dn for search
                    (optional if LDAP_BASEDN set in environment)
        filter:     LDAP search filter
        attributes: whitespace-separated list of attributes to retrieve
                    (if no attribute list is specified, all are retrieved)


Last edited by pludi; 07-27-2010 at 06:05 AM.. Reason: code tags, please...
# 4  
Old 07-27-2010
DN, in my previous example, is not part of the search, DN is the attribute you want returned on any object that satisfies the search.

If you do NOT specify 'any' attributes, you will get 'all' attributes back.

I do see that I made a mistake in translating from "my script" to
the example script ... by using the single quotes, instead of double quotes.

Here's what I'm using which works. It requests the samaccountname and DN be returned on any object that matches the search:

Code:
/usr/local/dms/openldap-2.2.13/bin/ldapsearch -v -x -W \
       -H ldap://my_ad.domain_controller_name.mycorp.com       \
       -b dc=mycorp,dc=com                                                 \
       -D cn=my_samaccoutname,cn=users,dc=mycorp,dc=com   \
        "(&(objectclass=user)(samaccountname=$search_user))"   \
        DN samaccount

You may be using a newer version of openldap than I am,
and it may be "forcing" you to specify a "scope". Just add
Code:
-s sub

following the
Code:
-v

in my example above.

NOTE: The example above will "prompt" you for your
active-directory password.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find if a User exist if not create user

What I'm trying to do is write a script in Perl to find a user and if that user exist it would print "User Exist, Pls Try Again". If The user doesn't exist I'm able to create a user with a password. Any suggestions? (3 Replies)
Discussion started by: GoBoyGo
3 Replies

2. Shell Programming and Scripting

LDAPSEARCH Return NULL User Attributes

Using ldapsearch to return entries from OID for specific attributes. However, if an attribute is null the field value is not returned. Is there anyway to force ldap to return field names in all cases? LDAP: ldapsearch -T -v -h aaaaa -p 111 -D "cn=orcladmin" -w "xxxx" -L -b... (0 Replies)
Discussion started by: u20sr
0 Replies

3. Solaris

Ldapsearch command not exiting

All, New to this forum and unix here so please forgive any silly questions. I am on a Solaris 10 machine and need a script to query our Windows Domain controller to determine if a user is active or not. Man files were helpful enough and it worked. The Challenge is the program doesn't exit... (1 Reply)
Discussion started by: daniel333
1 Replies

4. Red Hat

Ldapsearch problem

Hi I'm not familiar with ldap and I hope someone to help me. I need to get some attribute value from ldap DB. When i run the following command is OK: ldapsearch -h localhost -p 16611 -x -D cn=user -w passwd -b msisdn=359502479649,dc=MSISDN,DC=C-NTDB "(objectClass=SUBINNSS)" refinmocNAME the... (0 Replies)
Discussion started by: vasil
0 Replies

5. Shell Programming and Scripting

How to find DL Owner info using ldapsearch?

Currently i have following syntax: ldapsearch -D "CN=..,OU=..,OU=All Businesses,DC=..,DC=..,DC=.." -w .. -h .. -p .. -b "OU=All Businesses,DC=..,DC=..,DC=.." "managedObjects=$DL_NAME_CN" employeeNumber givenName sn -S employeeNumber -x which gives me following info: "requesting:... (0 Replies)
Discussion started by: arsenghani
0 Replies

6. UNIX for Dummies Questions & Answers

Need assistance on using ldapsearch

Never knew of this command ldapsearch, but I would like to use it to lookup a single user and return where their office is. Is this possible? I'm totally starting from scratch. I already saw some of the gurus say read the man page, which is pretty greek when you don't know the details of... (1 Reply)
Discussion started by: srhadden
1 Replies

7. UNIX for Advanced & Expert Users

ldapsearch to find netgroups which a user belongs to

how to use ldapsearch to find all the netgroups a user belongs to? It's Solaris. (1 Reply)
Discussion started by: jalite19
1 Replies

8. Programming

LDAPSEARCH in C

Hi all, I've been searching for ldapsearch function these few days. Still I couldn't found the solution. I would like to translate this query ldapsearch -x -LLL -h new_IP -p new_Port -D "cn=jw" -w "dummy" -b "id=2311,o=WC,c=jp" -s sub to ldap_search_ext_s() function in C program. ... (0 Replies)
Discussion started by: tailangong
0 Replies

9. UNIX for Advanced & Expert Users

Counting the ldapsearch results

Hi, Is there way to count the number of results in the ldapsearch, looking at the manpages i dont see an option, Using the following ldapsearch command to list attribute (User-Id=100) under my ObjectClass=my-Process, returns the entries matching the User-Id=100, and the problem i face here is... (0 Replies)
Discussion started by: ennstate
0 Replies

10. UNIX for Advanced & Expert Users

ldapsearch and XML

Hello all I would like to know if it's possible to extract users from Active Directory and parse the output to obtain a XML file with specific format. So the AD is a windows machine, and I would like to extract from a red hat linux serveur. I try ldapsearch command and ok I'm abble to extract... (5 Replies)
Discussion started by: ldiaz2106
5 Replies
Login or Register to Ask a Question