On Thu, Aug 5, 2010 at 4:09 PM, Rob Coops <[email protected]> wrote:
> On Thu, Aug 5, 2010 at 9:52 AM, sync <[email protected]> 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' => '[email protected]
> ',
> > '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
>
First , thanks for your and other's suggestions .
But now i have that problem as before .
I wrote another script to query the LDAP User information , the script is
these :
t...@xxx~: cat query_ldap.pl
--------------------------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl
use Net::LDAP;
$ldap = Net::LDAP->new ("localhost") or die "$@";
$ldap->bind;
$mesg = $ldap->search(base => "dc=example,dc=com", filter =>
"(objectClass=organizationalPerson)");
@entries = $mesg->entries;
$ldap_num = $mesg->count;
foreach $entry (@entries){
$entry->dump;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
Then I run this command to run that script:
t...@xxx ~: chmod +x query_ldap.pl
t...@xxx ~: ./query_ldap.pl
dn:uid=test1,ou=People,dc=example,dc=com
uid: test1
cn: test1
sn: test1
userPassword: 123456
gidNumber: 100
homeDirectory: /home/test1
objectClass: person
organizationalPerson
inetOrgPerson
posixAccount
uidNumber: 577
dn:uid=test2,ou=People,dc=example,dc=com
uid: test2
cn: test2
sn: test2
userPassword: 123456
gidNumber: 100
homeDirectory: /home/test2
objectClass: person
organizationalPerson
inetOrgPerson
posixAccount
uidNumber: 578
dn:uid=test3,ou=People,dc=example,dc=com
uid: test3
cn: test3
sn: test3
userPassword: 123456
gidNumber: 100
homeDirectory: /home/test3
objectClass: person
organizationalPerson
inetOrgPerson
posixAccount
uidNumber: 579
...
...
I refered to " Chas. Owens" suggestion to add this line "die "there are no
entries" unless @entries;"
after "my @entries = $mesg->sorted('uidNumber')" line in the
add_users.plscript.
Then i run that script , the screen shows that wrong message : "There are
no entries ....".
I don't understand why it is that result . Because there exists that
"uidNumber" line in the LDAP user information ....
Thanks in advance ....