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

Hi Monks,

I am performing search on AD server and retrieving data from the Directory. I have CSV file with values and I am using the fifth field ($data) as search string for Net::Ldap and storing sAMAccountName in $uid.

Now when the AD attribute has a trailing whitespace in the extensionAttribute1 value the search fails. If i add a trailing whitespace in search string (i.e $data) the search succeeds. here is the code fragment

my @fields = split "," , $line; my $data = $fields[5]; my $uid; my $filter = "(extensionAttribute1=$data)"; my $result = $ldap->search ( base => 'DC=Infoedge,DC=com', filter => $filter, ); foreach my $entry ($result->entries){ $uid=$entry->get_value('sAMAccountName'); }

How can I ask Net::Ldap to search for value even if a trailing whitespace is present in AD attribute extensionAttribute1.

Replies are listed 'Best First'.
Re: Net::LDAP search attribute with whitespace in AD
by AppleFritter (Vicar) on Oct 26, 2015 at 10:30 UTC

    Howdy, and welcome to the Monastery!

    I've never used Net::LDAP (or any kind of LDAP), but looking at its and Net::LDAP::Filter's documentation, I think that there is no such thing as a flag to ignore whitespace.

    Unfortunately filters can't contain regular expressions either (nobody ever takes a cue from Perl, it seems!), but they can use alternation, so you could use (|(extensionAttribute1=$data)(extensionAttribute1=$data )) as a filter and see if that works. Needless to say this will only catch a single whitespace. (And it's untested to boot.)

    While I'm commenting, BTW, I really recommend using a module such as Text::CSV for handling CSV files. There's no need to reinvent the wheel, and code such as

    my @fields = split "," , $line; my $data = $fields[5];

    will break in the presence of quoted strings containing commas.

      Hello, Thanks for helping me on this, I will try your solution. Also the CSV file is generated from another Bash script on the server. It has comma separated value's without quotes.