On Thu, Aug 5, 2010 at 9:52 AM, sync <jian...@gmail.com> wrote:

> Hi, guys:
>
> i have a perl script that supposed to add users to  ldap . when i run the
> script it get:
>
> Can't call method "get_value" on an undefined value at ./add_user.pl
>
>
>
> Any help will be appreciated?
>
>
> The  following message is my  perl script message   and my perl version is
> 5.8.8 on CentOS 5.4 x86_64.
>
> t...@xxx ~: cat add_user.pl
>
> --------------------------------------------------------------------------------------------------------------
> #!/usr/bin/perl
>
> use strict;
>
> use Net::LDAP;
>
>
> die "Usage is adduser.pl [username] [realname]\n" if length(@ARGV) != 1;
>
> my $username = $ARGV[0];
> my $realname = $ARGV[1];
>
> my $ldap = Net::LDAP->new('localhost');
> my $mesg = $ldap->bind;
> my $mesg = $ldap->search(
>                      base => "ou=People,dc=example,dc=com",
>                      filter => "(uid=$username)",
>                  );
> $mesg->code && die $mesg->error;
>
>
> my $searchResults = $mesg->count;
> die "Error! Username already exists!" unless $searchResults == 0;
>
> #print $searchResults;
>
> $mesg = $ldap->search(
>                  base => "ou=People,dc=example,dc=com",
>                  attrs => ['uidNumber'],
>              );
>
> my @entries = $mesg->sorted('uidNumber');
> my $entry = pop @entries;
>
> my $newuid = $entry->get_value( 'uidNumber');
> $newuid++;
>
> my $result = $ldap->add ("uid=$username,ou=People,dc=example,dc=com",
>                  attr => [ 'cn'              => $realname,
>                              'uid'             => $username,
>                              'uidNumber'       => $newuid,
>                              'mail'            => '$usern...@example.com',
>                              'homeDirectory'   => '/home/$username',
>                              'objectclass'     => ['person',
> 'inetOrgPerson', 'posixAccount']
>                              ]
>
>                          );
>
>
> $mesg = $ldap->unbind;
>


Ok, so the line that is causing this error is: my $newuid =
$entry->get_value( 'uidNumber');
Which is the only get_value call you make. This must mean that entry is
simply empty or at least doesn't contain what you expected it to contain.

What you are doing is first a search for anything with uidNumber in
the: ou=People,dc=example,dc=com
base. Rather then checking for error values or even verifying that anything
actually was returned you instantly call: my @entries =
$mesg->sorted('uidNumber');

For all we know @entries might be completely empty.

I would add some checking here and there, first check to see that there
where no error's after your search. Then make sure that at least some
results was returned. If this is the case then sort your search results and
then after all of that, take one entry and first of all dump it all to
STDOUT or to a log file (for debugging such a little bit of code STDOUT
should de just fine. If that looks like an entry that you expected to see
then try and call get_value and I can promise you that it will work just
fine.

Regards,

Rob

Reply via email to