Help with output from ldapsearch command


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with output from ldapsearch command
# 1  
Old 10-07-2016
Help with output from ldapsearch command

Using ldapsearch, I've built the following command to get what I need.

Code:
ldapsearch -x -LLL -E pr=200/noprompt -h abc-loc.somecompany.com -D "account@somecompany.com" -w password -b "ou=End Users,ou=Accounts,dc=abc,dc=somecompany,dc=com" -s sub '(distinguishedName=CN=Bob\\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com)' displayName saMAccountName telephoneNumber mail department departmentNumber l st

Results are:

Code:
dn: CN=Bob\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com
l: sometown
st: somestate
telephoneNumber: (123)456-7890
displayName: Bob, Billy J
department: Handform
sAMAccountName: b123456
mail: billy.j.bob@somecompany.com
departmentNumber: 5432

Now, I want to do something with this output, for example, write to a .txt file in a format such as:

Code:
Bob, Billy J|b123456|(123)456-7890|billy.j.bob@somecompany.com|Handform|5432|Somestate|Sometown

I know I can redirect the output of the ldapsearch to a file, but I'm not sure what the best route would be to format the output into a line similar to what I've shown above. Any pointers/opinions would help at this point. Thanks!


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-07-2016 at 02:29 PM.. Reason: Added CODE tags.
# 2  
Old 10-07-2016
You could pipe the output through a text utility like sed, awk, or others, to format it to desire.
# 3  
Old 10-07-2016
Quote:
Originally Posted by RudiC
You could pipe the output through a text utility like sed, awk, or others, to format it to desire.
Thanks RudiC! I'll do some research on sed and awk.
# 4  
Old 10-07-2016
Try
Code:
ldapsearch ... | 
awk -F: '
NR == 1 {n = split (FLDS, T)
        }
        {OUT[$1] = substr ($2, 2)
        }
END     {for (i=1; i<=n; i++)   {printf "%s%s", ODL, OUT[T[i]]
                                 ODL = "|"
                                }
         printf RS
        }
' FLDS="displayName:sAMAccountName:telephoneNumber:mail:department:departmentNumber:l:st"
Bob, Billy J|b123456|(123)456-7890|billy.j.bob@somecompany.com|Handform|5432|sometown|somestate

# 5  
Old 10-07-2016
Thanks RudiC. That worked. Now, to break it all down to get a good understanding of how awk works. I really do appreciate your help.

Thanks again.
# 6  
Old 10-07-2016
May i ?

Hope below information helps. I have mapped code line number and explanation for the same.

Code:
1) ldap | 
2) awk -F: '
3) NR == 1 
4) {n = split (FLDS, T)
        }
5)        {OUT[$1] = substr ($2, 2)
        }
6) END     
7) {for (i=1; i<=n; i++)   {printf "%s%s", ODL, OUT[T[i]]
8)  ODL = "|"
    }
9)         printf RS
}
10) ' FLDS="displayName:sAMAccountName:telephoneNumber:mail:department:departmentNumber:l:st"


Quote:
1) Pipe the output
2) start awk and set Field Seperator as colon ie ":" .
3) NR tracks input line/record. When NR is equal to 1, use inbuilt split function.
4) split(string,arrayname,seperator).splits the string in to arrayname as per given fieldseperator.
Here split FLDS variable and stores them in array named "T" index starting from 1 ie T[1]="displayName", T[2]="sAMAccountName" etc.Note that ,split function fieldseperator is omitted here. Hence fieldseperator from -F is used ie colon. array T size is saved in variable named "n".
5) Here another array named OUT with its index is first field (from ldapsearch output) and its value is starting from second in second field .substr(string,start,length) - Returns as per length character , starting character from "start" in string called "string". Here length is omitted.Hence , it will return starting from second character and rest of the string.
6) END block will be executed after completion of all input lines/records are processed.
7) using for loop , to iterate through inner array (as per T array from split).
When i=1,T[1] gets displayName. Then array OUT contains, OUT[displayName]. Now, it prints using printf function, value of variable
OUT[displayName] here.
8) ODL = "|" # ODL is set to |
9) RS is Record Seperator . Default is newline.
10) Pass the variable named FLDS filled with value.
# 7  
Old 10-07-2016
This is perfect! Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. 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

3. 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

4. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

useless line feeds in ldapsearch output. Howto remove with shell script?

Hi $ cat ad.sh ldapsearorg -x -LLL -h sb1131z.testbadbigcorp.org -D "CN=ADMINZZ,OU=AdminRoles,DC=testbadbigcorp,DC=org" -w "UT3w4f57lll--4...4" -b "OU=Test,DC=testbadbigcorp,DC=org" "(&(&(&(&(objectCategory=person)(objectClass=user)(lockoutTime:1.2.840.113556.1.4.804:=4294967295)))))" dn$... (3 Replies)
Discussion started by: slashdotweenie
3 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. 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

8. 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

9. UNIX for Dummies Questions & Answers

ldapsearch time limit

We have an application that uses Active directory to authenticate the users. the admins of the app. were complaining because the windows domain controller they are going against is not very stable. I wrote a shell script using ldapsearch to look up a user against the domain controller their app... (2 Replies)
Discussion started by: jayjabour
2 Replies

10. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies
Login or Register to Ask a Question