schwende has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use Net::LDAP; use Time::Piece; if ($#ARGV < 0) { print STDERR "Usage: ./everyone.pl pwd\n"; exit 1; } my $host = "ldap1.server.com:1000"; my $bindcred = $ARGV[0]; my $binddn = "cn=Directory Manager"; my $basedn = "ou=People,dc=server,dc=com"; my $dateout = Time::Piece->new->strftime('%Y%m%d'); my $infile = "output/$dateout/everyone.list"; open(OUTFILE, ">", $infile) or die("Can't open $infile.clean for outpu +t"); my $ldap = Net::LDAP->new($host, timeout=>2) || SYSerror("New", "Couldn't connect to $host"); my $mesg = $ldap->bind("$binddn", password=>"$bindcred"); if ($mesg->is_error) {LDAPerror("Bind", $mesg);} my $result = $ldap->search(base=>"$basedn", scope=>"sub", filter=>"(&(|(servercomBannerRole=Student)(servercomBannerRole=Facul +ty)(&(servercomBannerRole=Staff)(!(servercomBannerRole=Villagestaff)) +)))", attrs=>["mail","cn"]); if ($result->is_error) {LDAPerror("Search", $result);} my @entries = $result->entries; my $mExcludingFile = "excludes/everyone.txt"; my $includingFile = "includes/everyone.txt"; my @mArray = (); my $mLine; my $mCountAll = 0; my $mCountOrig = 0; my $mCountAdd = 0; my $mCountDel = 0; open(ROFILE, "<", $mExcludingFile) or die("Can't open $mExcludingFile +exception file!"); while(<ROFILE>) { push(@mArray, $_); } close(ROFILE); my $mArrayNow; my $entry; foreach $entry(@entries) { my $name = $entry->get_value("cn"); my $email = $entry->get_value("mail"); my $mLineNow = "$email $name\n"; my $mOkay = 1; $mCountOrig++; foreach $mArrayNow (@mArray) { if ($mLineNow eq $mArrayNow) { $mOkay = 0; } } if ($mOkay == 1) { print OUTFILE $mLineNow; } else { $mCountDel++; } } flock(OUTFILE, 8); close(OUTFILE); open(OUTFILE, ">>", $infile); open(EXCEPTF, "<", $includingFile) or die("Can't open $includingFile i +nclusion file!"); while(<EXCEPTF>) { print OUTFILE $_; $mCountAdd++; } flock(OUTFILE, 8); close(OUTFILE); close(EXCEPTF); $mCountAll = $mCountOrig + $mCountAdd - $mCountDel; print "Ok.. Generated ".$mCountAll." (".$mCountOrig." + ".$mCountAdd. +" - ".$mCountDel.")\n"; sub LDAPerror { my ($from, $mesg) = @_; print "Error: " . $mesg->code . " (" . $mesg->error_name . ")\n"; print $mesg->error_text . "\n"; } sub SYSerror { my ($from, $mesg) = @_; print "Error: $mesg\n"; print "Source: $from\n"; die "$@"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: script runs, but doesn't appear to call file
by Corion (Patriarch) on Jul 17, 2023 at 17:26 UTC | |
|
Re: script runs, but doesn't appear to call file
by haukex (Archbishop) on Jul 17, 2023 at 17:36 UTC | |
|
Re: script runs, but doesn't appear to call file
by schwende (Initiate) on Jul 18, 2023 at 14:49 UTC |