Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bash to parse the output of ldapsearch

Tags:

bash

sed

ldap

I recently wrote a bash script that had to parse the output of ldapsearch results. The script works, but I imagine there is a more efficient way to accomplish this.

The script executes an ldapsearch command, which outputs multiple records that are in a multiline format. Each record is separated by a blank line. What I ended up doing was the following:

  1. add a delimitating character to the end of each line
  2. Add the string 'DELIM' to blank lines
  3. trimmed all new lines
  4. Replaced 'DELIM' with a new line

What this effectively did was turn the multiline output of ldapsearch to multiple lines of delimited separated values. I then use cut twice to parse the lines (once to split the delimiter, and then again to spit the output of the ldap result)

Here is the code:

while IFS= read -r line ; do
 dn=$(echo "$line" | cut -d '#' -f 1 | cut -d " " -f 2)
 uid=$(echo "$line" | cut -d '#' -f 2 | cut -d " " -f 2)
 uidNumber=$(echo "$line" | cut -d '#' -f 3 | cut -d " " -f 2)
 gidNumber=$(echo "$line" | cut -d '#' -f 4 | cut -d " " -f 2)

 # Code emitted since it's not relevant

done < <(ldapsearch -x -H "$ldap_server" -D 'cn=Directory Manager' -w $ds_password -b "$searchbase" -LLL uid uidNumber gidNumber | sed 's/$/#/g' | sed 's/^#$/DELIM/g' | tr -d '\n' | sed 's/DELIM/\n/g')

The output of the ldapsearch command is the following

dn: uid=userone,ou=People,dc=team,dc=company,dc=local
uid: userone
uidNumber: 5000
gidNumber: 5000

dn: uid=usertwo,ou=People,dc=team,dc=company,dc=local
uid: usertwo
uidNumber: 5001
gidNumber: 5001

Is there a more efficient way to accomplish this? Specifically one that doesn't use piping so extensively?

like image 536
Dave Avatar asked Sep 07 '25 04:09

Dave


1 Answers

You can play around with awk

ldap search | awk '$1=$1' RS= ORS='\n' OFS='#'
dn: uid=userone,ou=People,dc=team,dc=company,dc=local#uid: userone#uidNumber: 5000#gidNumber: 5000
dn: uid=usertwo,ou=People,dc=team,dc=company,dc=local#uid: usertwo#uidNumber: 5001#gidNumber: 5001

RS= Set record selector to nothing (will set awk to block mode) (normally every line)
ORS='\n' Set output record selector to a new line
OFS='#' set output field separator to #
$1=$1 recreate the line and print it.

This may be more secure to use:

awk '{$1=$1}1' RS= ORS='\n' OFS='#'
like image 93
Jotne Avatar answered Sep 10 '25 00:09

Jotne