format results of duplicate ldapsearch records

Discussion in 'Perl Misc' started by marcorice@gmail.com, Jul 15, 2005.

  1. Guest

    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
    , Jul 15, 2005
    #1
    1. Advertising

  2. wrote in news:1121444850.674340.90820
    @o13g2000cwo.googlegroups.com:

    > I a so new to perl it's not even funny,


    Don't post the same message multiple times.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 16, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sumit
    Replies:
    1
    Views:
    6,438
    Anatoly
    Nov 25, 2003
  2. Jameel
    Replies:
    1
    Views:
    559
  3. Nico Grubert

    ldapsearch example in python-ldap?

    Nico Grubert, Nov 23, 2006, in forum: Python
    Replies:
    1
    Views:
    3,982
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Nov 23, 2006
  4. Paul Naude

    Urgent: Records duplicate on update!

    Paul Naude, Feb 21, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    109
    Paul Naude
    Feb 21, 2006
  5. Replies:
    0
    Views:
    156
Loading...

Share This Page