sync wrote:
Hi, guys:

Hello,

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

You should also have:

use warnings;

use strict;

use Net::LDAP;


die "Usage is adduser.pl [username] [realname]\n" if length(@ARGV) != 1;

if length(@ARGV) != 1

Is the same as saying:

if @ARGV >= 10

Is that what you really meant?

length() evaluates @ARGV in scalar context and an array in scalar context returns the number of elements in the array. So if @ARGV has 0 through 9 elements that single digit has a length of 1, and if @ARGV has 10 or more elements then the length will be 2 for a 2 digit number and 3 for a 3 digit number, etc.


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;

If you don't need @entries[ 1 .. $#entries ] then just do:

my ( $entry ) = $mesg->sorted( 'uidNumber' );


my $newuid = $entry->get_value( 'uidNumber');
$newuid++;

Why two steps:

my $newuid = 1 + $entry->get_value( 'uidNumber' );


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;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to