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
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