rc has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

I'm trying to use ldapsearch to query AD and parse the results using perl. I'm having trouble with the multivalue attributes like proxyAddresses; I'm only getting the first value returned.

##### Edit these variables ##### my $delim = "*!*"; my $attributes = "cn displayName employeeID extensionAttribute3 extensionAttribute1 mail mailNickname proxyAddress +es userPrincipalName"; my $server = "server1"; my $basedn = "dc=mycorp,dc=com"; ################################ ...snip... $/ = ''; #Paragraph mode for input my @atts = split(/ /, $attributes); my $cmd = "ldapsearch $flags -h $server -b \"$basedn\" \"(mail=*)\" $a +ttributes"; open(SEARCH, "$cmd |") or die "\nError: $!\n"; while(<SEARCH>) { my $line; foreach my $att (@atts) { $line .= lc(/^$att: *(.*)$/im ? $1 : '') . $delim; } } This is the data that ldapsearch returns: dn: CN=Connors\, Ralph,OU=Users,DC=mycorp,DC=com cn: Connors, Ralph displayName: Connors, Ralph mail: rconnors@mycorp.com employeeID: 1234567 proxyAddresses: MBX:1 proxyAddresses: smtp:rconnors@mailgw proxyAddresses: SMTP:rconnors@mycorp.com proxyAddresses: X400:c=US;a= ;p=mycorp;o=NA;s=Chastain;g=Robert; proxyAddresses: RFAX:Connors, Ralph@ userPrincipalName: rconnors@mycorp.com extensionAttribute1: 0012A extensionAttribute3: 1234567 mailNickname: rconnors

I forgot to mention I'm setting $/ = ''; for paragraph mode.

This works fine for all but the multivalue attributes.

What am I missing wise ones?

Thanks, RC

Replies are listed 'Best First'.
Re: parsing ldapsearch results
by sgifford (Prior) on May 03, 2004 at 16:50 UTC

    First, why don't you use Net::LDAP? It will take care of parsing for you, provide a more native interface to LDAP, and is likely to be faster.

    If you can't for some reason, pasting in a sample nonworking ldapsearch command and its output would let more people help you. You really just have a parsing problem, and if you give us some sample data you can get help even from people who don't know (or remember) the specifics of the ldapsearch command.

    Update: I remembered ldapsearch's syntax better. The problem is that your regexp is only asking for the first match. To get them all, wrap it in a while loop and use the g modifier:

    while (/^$att:\s*(.*)$/img) { my $val = $1; print "$att is '$val'\n"; }
Re: parsing ldapsearch results
by NetWallah (Canon) on May 03, 2004 at 18:50 UTC
    Most likely, your data is being returned in a single output message separated by "\n".

    This means that your while(<SEARCH>) loops only ONCE.

    To fix this, you will need to split $_ into multiple lines, then scan each line for your @atts.

    BTW, why didn't you use qw to declare and populate @atts ? You could easily have used join to make your command line ...

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.