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";
open(UIDs_n_IDs_NOTMATCH,">$UIDs_n_IDs_NOTMATCH") or die ("Can't open
$UIDs_n_IDs_NOTMATCH: $!\n");
my $UIDs_n_IDs_MATCH="./UIDs_n_IDs_Match";
open(UIDs_n_IDs_MATCH,">$UIDs_n_IDs_MATCH") or die ("Can't open
$UIDs_n_IDs_MATCH: $!\n");
my $UIDs_MATCH_But_Not_IDs="./UIDs_MATCH_But_Not_IDs";
open(UIDs_MATCH_But_Not_IDs,">$UIDs_MATCH_But_Not_IDs") or die ("Can't
open $UIDs_MATCH_But_Not_IDs: $!\n");
my $UIDs_NOTMATCH_But_IDs_MATCH="./UIDs_NOTMATCH_But_IDs_MATCH";
open(UIDs_NOTMATCH_But_IDs_MATCH,">$UIDs_NOTMATCH_But_IDs_MATCH") or
die ("Can't open $UIDs_NOTMATCH_But_IDs_MATCH: $!\n");
my $UIDs_BLANK_n_LOGIN_NOTMATCH="./UIDs_BLANK_n_LOGIN_NOTMATCH";
open(UIDs_BLANK_n_LOGIN_NOTMATCH,">$UIDs_BLANK_n_LOGIN_NOTMATCH") or
die ("Can't open $UIDs_BLANK_n_LOGIN_NOTMATCH: $!\n");
my $UIDs_n_IDs_NOTFOUND="./UIDs_n_IDs_NOTFOUND";
open(UIDs_n_IDs_NOTFOUND,">$UIDs_n_IDs_NOTFOUND") or die ("Can't open
$UIDs_n_IDs_NOTFOUND: $!\n");
my $TEMPFILE="./TEMPFILE";
open(TEMPFILE,">$TEMPFILE") or die ("Can't open $TEMPFILE: $!\n");

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.
if (("$unixuid" eq "$ntuid") && ("$unixid" eq "$ntid")) {
print "UnixUserID | $unixid | UID | $unixuid | MATCHES LOGIN
and UID found in LDAP | ldapusername | $ntid | NT_UserID | $ntuid |
name | $givenname,$mi
ddlename,$lastname | employeeid | $employeeid | email | $mail |
telephone | $telenum | DOMAIN | $domain\n";
print UIDs_n_IDs_MATCH "UnixUserID | $unixid | UID | $unixuid |
matches LOGIN and UID found in LDAP | ldapusername | $ntid | NT_UserID
| $ntuid | name
| $givenname,$middlename,$lastname | employeeid | $employeeid | email
| $mail | telephone | $telenum | DOMAIN | $domain\n";
}
elsif (("$unixuid" ne "$ntuid") && ("$unixid" ne "$ntid") &&
("$ntuid" ne "")) {
print "UnixUserID | $unixid | UID | $unixuid | does NOT
MATCH LOGIN and UID found in LDAP | ldapusername | $ntid | NT_UserID |
$ntuid | name | $g
ivenname,$middlename,$lastname | employeeid | $employeeid | email |
$mail | telephone | $telenum | DOMAIN | $domain\n";
print UIDs_n_IDs_NOTMATCH "UnixUserID | $unixid | UID |
$unixuid | does not match LOGIN and UID found in LDAP | ldapusername |
$ntid | NT_UserID |
$ntuid | name | $givenname,$middlename,$lastname | employeeid |
$employeeid | email | $mail | telephone | $telenum | DOMAIN |
$domain\n";
}
elsif (("$unixuid" ne "$ntuid") && ("$unixid" eq "$ntid") &&
("$ntuid" ne "")) {
print "UnixUserID | $unixid | UID | $unixuid | MATCHES
LOGIN but NOT UID found in LDAP | ldapusername | $ntid | NT_UserID |
$ntuid | name | $give
nname,$middlename,$lastname | employeeid | $employeeid | email | $mail
| telephone | $telenum | DOMAIN | $domain\n";
print UIDs_MATCH_But_Not_IDs "UnixUserID | $unixid | UID |
$unixuid | matches LOGIN but NOT UID found in LDAP | ldapusername |
$ntid | NT_UserID
| $ntuid | name | $givenname,$middlename,$lastname | employeeid |
$employeeid | email | $mail | telephone | $telenum | DOMAIN |
$domain\n";
}
elsif (("$unixuid" eq "$ntuid") && ("$unixid" ne "$ntid")) {
print "UnixUserID | $unixid | UID | $unixuid | MATCHES
LOGIN but not UID found in LDAP | ldapusername | $ntid | NT_UserID |
$ntuid | name | $give
nname,$middlename,$lastname | employeeid | $employeeid | email | $mail
| telephone | $telenum | DOMAIN | $domain\n";
print UIDs_NOTMATCH_But_IDs_MATCH "UnixUserID | $unixid |
UID | $unixuid | MATCHES LOGIN but not UID found in LDAP | ldapusername
| $ntid | NT_Us
erID | $ntuid | name | $givenname,$middlename,$lastname | employeeid |
$employeeid | email | $mail | telephone | $telenum | DOMAIN |
$domain\n";
}
elsif (("$unixuid" ne "$ntuid") && ("$unixid" ne "$ntid") &&
("$ntuid" eq "") && ("$ldapusername" ne "")) {
print "UnixUserID | $unixid | UID | $unixuid | does NOT
MATCH LOGIN and UID is BLANK in LDAP | ldapusername | $ntid | NT_UserID
| $ntuid | name |
$givenname,$middlename,$lastname | employeeid | $employeeid | email |
$mail | telephone | $telenum | DOMAIN | $domain\n";
print UIDs_BLANK_n_LOGIN_NOTMATCH "UnixUserID | $unixid |
UID | $unixuid | does NOT MATCH LOGIN and UID is BLANK in LDAP |
ldapusername | $ntid |
NT_UserID | $ntuid | name | $givenname,$middlename,$lastname |
employeeid | $employeeid | email | $mail | telephone | $telenum |
DOMAIN | $domain\n";
}
else {
print "UnixUserID | $unixid | *****not found in LDAP***** |
UNIXNAME | $fullname | UID | $unixuid\n";
print UIDs_n_IDs_NOTFOUND "UnixUserID | $unixid | *****not
found in LDAP***** | UNIXNAME | $fullname | UID | $unixuid\n";
}

}
}

close (UIDs_n_IDs_MATCH);
close (UIDs_n_IDs_NOTMATCH);
close (UIDs_MATCH_But_Not_IDs);
close (UIDs_NOTMATCH_But_IDs_MATCH);
close (UIDs_BLANK_n_LOGIN_NOTMATCH);
close (UIDs_n_IDs_NOTFOUND);
#close (TEMPFILE);
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top