Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Perl LDAP script error "Can't call method "dn" on an undefined value"

by anakin30 (Acolyte)
on Apr 03, 2013 at 04:53 UTC ( #1026775=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

i received below error when i run my code

The script generate result if the input file contains less then 10 users, however the error trigger if the input file contain more than 10 users

i appreciate if anybody can advice, how to rectify the error

Can't call method "dn" on an undefined value at ExportAttribute2.pl line 45, <TEST> line 991.

i have marked the error , line number with the comment # in the below code for your reference

$TEST = 'C:\Perl\project\export\20130402\test.txt'; #$TEST = 'C:\Perl\project\export\20130402\users.txt'; $outputfile = 'C:\Perl\project\export\20130402\result.csv'; open (FH, ">$outputfile") or die "$!"; print FH "uid,SN,GIVENNAME,uniSALUTATION,Displayname\n"; my $adHost = "ldap.uni.com"; my $adUsr = "uid=myscript,ou=users,o=uni.com"; my $adPass = "Myscript2012"; my $query = "(objectclass=uniperson)"; my @attrs = qw(uid SN GIVENNAME uniSALUTATION Displayname); my $ldap = Net::LDAP->new($adHost) or die $!; my $mesg = $ldap->bind($adUsr, password =>$adPass); my $page = Net::LDAP::Control::Paged->new( size => 1000 ) or die $!; open(TEST) or die("Could not open log file."); foreach $line (<TEST>) { chomp($line); #print "$line \n";#} #while (1) { my @args = ( base => 'ou=users,o=uni.com' , scope => 'sub', filter => +$line, attrs => \@attrs, control => [ $page ],); $mesg = $ldap->search ( @args ) or die $!; #while (my $entry = $mesg->shift_entry() ) { #uid,SN,GIVENNAME,uniSALUTATION,Displayname my $entry = $mesg->shift_entry(); my $entrydn = $entry->dn(); #line 45 the error triggered here my $uid = $entry->get_value ('uid'); my $SN = $entry->get_value ('SN'); my $GIVENNAME = $entry->get_value ('GIVENNAME'); my $uniSALUTATION = $entry->get_value ('uniSALUTATION'); my $Displayname = $entry->get_value ('Displayname'); print FH "$uid,$SN,$GIVENNAME,$uniSALUTATION,$Displayname\n"; }

please advice me how to get rid of the error above. I need to export 3000 users based on userID from input file with the selected attributes from LDAP using this script"

Comment on Perl LDAP script error "Can't call method "dn" on an undefined value"
Download Code
Re: Perl LDAP script error "Can't call method "dn" on an undefined value"
by Loops (Hermit) on Apr 03, 2013 at 05:41 UTC
    Hi there,

    Likely what is happening is that "$mesg" is not receiving valid data back from $ldap->search. You may just have a single bad entry in your test.txt file.

    At a minimum you will want to add a line that tests the value of $entry, like so:

    my $entry = $mesg->shift_entry(); next unless $entry;

    In order to skip over any bad entries in your input text file. Of course you should probably do proper error reporting as well.

    To debug this further you can use Data::Dumper and show the return results of the search function. If my guess is correct you will see 'errorMessage => "Bad filter"' in the returned hash:

    use Data::Dumper; ... $mesg = $ldap->search ( @args ) or die $!; print Dumper $mesg;
    HTH.

      Hi Loops,

      Thanks for sharing with me .. i appreciate that a lot.

Re: Perl LDAP script error "Can't call method "dn" on an undefined value"
by Loops (Hermit) on Apr 03, 2013 at 07:14 UTC
    In case it helps to see a working example, here's a stripped down version of your code configured to work with my universities public ldap server:
    use strict; use warnings; use Net::LDAP; use Net::LDAP::Util qw(ldap_explode_dn); my $ldap = Net::LDAP->new("ldap.sfu.ca:1389") or die $!; my $bind = $ldap->bind(); my @attr = (qw(sn givenName cn mail)); my @args = ( base => 'ou=people,o=SFU,c=CA', scope => 'sub', filter => '(objectclass=*)', attrs => \@attr); $,=','; my $mesg = $ldap->search ( @args ) or die $!; while (my $entry = $mesg->shift_entry()) { my $UID = ldap_explode_dn($entry->dn())->[0]->{UID}; print $UID, (map {$entry->get_value($_)} @attr), "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026775]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-07-31 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls