format results of duplicate ldapsearch records

M

marcorice

Hello all,
I a so new to perl it's not even funny, I started 3 weeks ago.
The code below works when I do an ldapsearch for a first and last name
from a ypcat passwd list and only one ldapsearch record is returned,
but when there are many records only the last record is processed. I
believe there should be a simple solution of either a while or foreach
condition but I CANNOT find it for the life of me. Here is the code and
the ldapsearch output below, CAN ANYONE HELP ME PLEASE.

#!/bin/perl
#
my $YP="REMOVED";
my $SHOST="REMOVED";
my $PORT="REMOVED";
my $UIDs_n_IDs_NOTMATCH="./UIDs_n_IDs_Not_Match";

my @ypsearch = (`ypcat passwd | grep user`);

# Search through the list of unix userIDs from ypcat and get
information
foreach (@ypsearch) {
($unixid, $NULL, $unixuid, $group, $namefield) = split /:/;
# split the gecos field and get fullname and employeeid
($FullName, $empid)=split (/\(|\,/, $namefield);
# further split the fullname with first middle and last name
($fname,$middle,$lname)=split (/ /, $FullName);
# Compensate for people with no middle name
if (!($lname))
{
$lname = $middle;
undef ($middle);
}
print "$unixid\n";
print "unixname $fname, $middle, $lname, $empid\n";
my @hash=();
# Do ldapsearch for user and capture specific fields
$info= (`ldapsearch -p $PORT -b DC=com -s sub -h $SHOST
"(&(sn=$lname)(givenName=$fname))" givenName middleName sAMAccountName
sn employeeID mail telep
honeNumber extensionAttribute13 userPrincipalName`);
print TEMPFILE "$info\n";
close (TEMPFILE);
chomp(@test=`cat $TEMPFILE`);
$/="";

foreach (@test){
($key, $value) = split /=/, $_;
;
if ($key =~ m/extensionAttribute13/){
$ntuid = $value;
}

elsif ($key =~ m/telephoneNumber/){
$telenum = $value;
}
elsif ($key =~ m/mail=/){
$mail = $value;
}
elsif ($key =~ m/employeeID/){
if ($value eq $empid) {
$employeeid = $empid;
}
else {
$employeeid = $value;
}
}
elsif ($key =~ m/sn/){
$lastname = $value;
}
elsif ($key =~ m/sAMAccountName/){
$ntid = $value;
}
elsif ($key =~ m/middleName/){
if ("$value" eq "$middle") {
$middlename = $middle;
}
else {
$middlename = $value;
}
}
elsif ($key =~ m/givenName/){
$givenname = $value;
}
elsif ($key =~ m/userPrincipalName/){
# Remove unwanted characters, probably better of doing it but.
$value =~ s/.*@//;
$value =~ s/\..*//;
$domain = $value;
}
next if $key eq "";

# Check to see if users have and or match IDs and UIDs and send it to
appropriate files.
DO if and else statements with the variables $ntid, $middle,
$givenname.....etc
 
A

A. Sinan Unur

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
I a so new to perl it's not even funny,

Don't post the same message multiple times.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top