ldap search function

2014-08-25 264 words 2 mins read

First you will need the ldap search utility. The client for ldap search comes in openldap-clients, so you need to install that first:

sudo yum install openldap-clients

Now, that you have installed it, try to find something in some open ldap server, example:

ldapsearch -LLL -h  db.debian.org   -x  -b "dc=debian,dc=org" "cn=Joao*"

This should list couple of entries for you. Now, that you have ldapsearch working, lets define a function in .bashrc file:

 

ldaps () 
{ 
    ( ldapsearch -LLL -h $2 -x -p 389 -b "$3" "cn=*$1*" | awk -F: 'BEGIN {
        last       = "NA"
        first      = "NA"
        name       = "NA"
        loc        = "NA"
        state      = "NA"
        postalcode = "NA"
        homephone  = "NA"
        telephonenumber = "NA"
        mail       = "NA"
        mobile     = "NA"
        printf(" last,first,full name,work#,e-mail,phone3\n");
}
/^sn: /              {last=$2}
/^givenName: /       {first=$2}
/^cn: /              {name=$2}
/^street: /          {address=$2}
/^l: /               {loc=$2}
/^st: /              {state=$2}
/^postalCode: /      {postalcode=$2}
/^homePhone: /       {homephone=$2}
/^telephoneNumber: / {telephonenumber=$2}
/^mail: /            {mail=$2}
/^mobile: /          {mobile=$2}
/^dn/ {
        if(last != "" && first != "" && last != "StoogeAdmin") printf("%s,%s,%s,%s,,%s,%s\n",last,first,name,telephonenumber,mail,mobile)
        last       = "NA"
        first      = "NA"
        name       = "NA"
        address    = "NA"
        loc        = "NA"
        state      = "NA"
        postalcode = "NA"
        homephone  = "NA"
        telephonenumber = "NA"
        mail       = "NA"
        mobile     = "NA"
}
# Capture last dn
END {
        if(last != "" && first != "" ) printf("%s,%s,%s,%s,,%s,%s\n",last,first,name,telephonenumber,mail,mobile)
}' ) | column -t -s ','
}

You might want to chagne few of the matches in the awk in the above script to suit your needs and then you can run this with :

ldaps    "searh string" "search host" "search base"

Related Articles:


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it