2

I'm not an experienced shell scripter, so I'm hoping for some expert help!

We use the following command in a for loop that cycles through UIDs:

ldapsearch -x -H ldaps://ldap-purple.example.com -b ou=People,dc=example,dc=com uid=jdoe

This command yields a record such as this:

# extended LDIF
#
# LDAPv3
# base <ou=People,dc=example,dc=com> with scope subtree
# filter: uid=jdoe
# requesting: ALL
#

# jdoe, people, example.com
dn: uid=jdoe,ou=people,dc=example,dc=com
cn: John Doe
homeDirectory: /afs/rats.example.com/users/t/jdoe
loginShell: /bin/bash
objectClass: posixAccount
uid: jdoe
uidNumber: 9239
gidNumber: 100002

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

What I'd like to do is be able to grep or awk (or whatever) to then generate the 'cn','uid', and 'uidNumber' on the same row, I.e.

John Doe, jdoe, 9239

I'm not sure what tool can be used to create this, however.

Thanks in advance your your help!

Cheers, Dan

4

3 に答える 3

2

Yes, easy with awk. Given that you say this is running in a loop, I'm sketching in a solution with a basic loop, that may or maynot match what you're doing. Hopefully you'll see how to adapt to your exact needs.

while read ou dc1 dc2 uid ; do
    dapsearch -x -H ldaps://ldap-purple.example.com -b ou="$ou",dc1="$dc1",dc="$dc2" uid="$uid" \
    | gawk -F":" '
        $1=="cn" { cn = $2; sub(/^  */,"", cn); next}
        $1=="uid" { uid = $2; sub(/^  */,"", uid);next}
        $1=="uidNumber" { uidNumber = $2; sub(/^  */,"", uidNumber );next}

        # ... etc
        END {
           printf("%s,%s,%s\n", cn,uid,uidNumber)
        }           
    '
done < /path/to/ldap_lookup.lst

output

John Doe,jdoe,9239

(Using your sample record above as input)

If for some reason you want the leading spaces in the records, take out the sub() cmds.

I don't have ldapsearch to test with. If you get error messages, gawk is pretty good about showing where the problem is, and if it's not exactly where it identifies the problem spot, then it's usually before that. Post error messages as comments and I'll see what I can do.

IHTH

于 2012-09-08T16:14:11.600 に答える
1
awk 'BEGIN { RS = "gidNumber: [0-9]+" ; FS = "\n" ; OFS=","}
{ for (i=1;i<=NF;i++) { 
       if ($i ~ "^cn: ") { cn=gensub("^cn: ","","g",$i) }
       if ($i ~ "^uidNumber: ") { uidNum=gensub("^uidNumber: ","","g",$i) }
       if ($i ~ "^uid: ") { uid=gensub("^uid: ","","g",$i) }
      }
  if ( cn > "" && uid > "" && uidNum > "" ) {
       print cn, uid, uidNum
      }
  cn="" ; uid="" ; uidNum=""
}' INPUTFILE

あなたのために働くかもしれません。ideone.comで実際の動作を確認してください(テスト用にもう1つレコードを追加しました)。

于 2012-09-08T18:03:47.930 に答える
1

Thanks for your answers, I'll try all of them!

Before I saw your replies, I scrapped the following together, that while ugly, does the trick:

ldapsearch -x -H ldaps://ldap-purple.example.com -b ou=People,dc=example,dc=com uid=$i | awk -F': ' '/uid:/ {ORS=",";print $2} /uidNumber/ {ORS="\n";print $2} /cn/ {ORS=",";split ($2,fields,"-"); print fields[1]}'
于 2012-09-09T00:29:16.540 に答える