Hi,

I’m novice in Perl. I wrote the below Perl script. I’ve some issue with foreach or while loop and it doesn’t work as expected.

tmp/dl


group1
group2

/tmp/removemem
uid1
uid2


Alias for uid1:
uid1@example.com
uid.1@example.com

Alias for uid2:
uid2@example.com
2.uid@example.com

1. Go through each group from /tmp/dl list and get the members from that group using ldap
2. Go through the user from list /tmp/removemem and get the aliases for each from ldap
3. go through each member(from step 1) email and grep against the array of aliases.
4. If matched print else continue to the next member

group1
—————
a1@example.com
uid1@example.com

group2
—————
a2@example.com
uid2@example.com
2.uid@example.com
uid.1@example.com
#! /usr/bin/perl -w use Net::LDAP; open ( FH, "<", "/tmp/dls" ) or die "Could not open file... :$! \n"; open ( FH2, "<", "/tmp/removemem" ) or die "Could not open file... :$! + \n"; $ldap = Net::LDAP->new ( “ldap-master" ) or die "$@"; $mesg = $ldap->bind( dn => "uid=admin,ou=SysAccounts,dc=admin +,dc=local", password => “pass", ); while ( $dl = <FH> ) { chomp $dl ; $mesg = $ldap->search ( base => “dc=example, dc=com", filter => "mail=$dl", attrs => [ 'mgrprfc822mailmember' ], ); @entries = $mesg->entries; foreach $entry ( @entries ) { @members = $entry->get_value('mgrprfc822mailmember'); print "\n"; print "$dl\n"; print "Members are: @members\n"; foreach $member ( @members ) { while ( $curmem = <FH2> ) { chomp $curmem; print "\n$curmem\n"; $mesg2 = $ldap->search ( base => “dc=example, dc=com", filter => "uid=$curmem", attrs => [ 'mailLocalAddress' ], ); @entries2 = $mesg2->entries; foreach $entry2 ( @entries2 ) { @current = $entry2->get_value('mailLocalAddress'); print "\n File UID $curmem has following Aliases: @curren +t\n"; # } print "MATCH Starting\n"; if ( grep /^$member$/, @current ) { print "MATCHED: $member\n" ; } # } } } } } } $mesg = $ldap->unbind ;

In reply to foreach/while loop help by bshah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.