[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] My first script



Congrats--quite a bit more than my first attempt! 

One suggestion I'd make if you're new to Perl is to make an early habit of
using single quotes (or even q(...)) around string constants
_where_possible_--like around that filter expression. As much as I love
using Perl, I can't count how many 'surprises'  I've had when it reached
into an expression it didn't need to. Plus, it helps you when performance
matters.

Also, LDAP logical ops allow more than two terms, which lets you drop a few
parentheses:

my $filter =
"(&(&(!(sn=System))(!(sn=Sharepoint*))(sn=*))(mail=*)(&(!(givenname=NET))(gi
venname=*)))";
# ...becomes...
my $filter =
'(&(mail=*)(sn=*)(givenname=*)(!(sn=System))(!(sn=Sharepoint*))(!(givenname=
NET)))';
# ...or even...
my $filter =
'(&(mail=*)(sn=*)(givenname=*)(!(|(sn=System)(sn=Sharepoint*)(givenname=NET)
))';

-Andy

-----Original Message-----
From: omaha-pm-bounces+ahadenfeldt=windstream.net@pm.org
[mailto:omaha-pm-bounces+ahadenfeldt=windstream.net@pm.org] On Behalf Of
Bill Brush
Sent: Thursday, March 08, 2012 12:06 PM
To: Perl Mongers of Omaha, Nebraska USA
Subject: [Omaha.pm] My first script

Well it's essentially finished, so I thought I'd share.  I'm sure it could
be improved, but honestly I'm just impressed I got it to work.
Now I just need to tweak it to work in unattended cron mode.

I sanitized the code so none of my particulars are there, for obvious
reasons.  Big thanks to Jay for pointing me towards the Email::Stuff module.

Bill

****************************************************************************
***********************************

#!/usr/bin/perl


use strict;
use Net::LDAP;
use IO::File;
use Email::Stuff;

#Creates connection to netusrA on the LDAP port, using the ldap_binder
account my $netad = Net::LDAP->new("myhost.mydomain") or die "Could not
connect! $!"; $netad->bind("bind account here", password=>"bind account
password here");

#Defines the LDAP search to start at the root of the directory tree my $base
= 'root of my LDAP directory tree';

#The filter is in LDAP format
#The filter specifies users that have an SN (last name) that exists, but is
not equal to System nor starts with Sharepoint #AND that have an email
address #AND that have a givenname (first name) that is not equal to NET my
$filter =
"(&(&(!(sn=System))(!(sn=Sharepoint*))(sn=*))(mail=*)(&(!(givenname=NET))(gi
venname=*)))";

# $attrs specifies which attributes to be retrieved my $attrs =
["givenname","sn","mail"];

# This creates a search result object ($results) from the netad search
method my $results =
$netad->search(base=>$base,filter=>$filter,attrs=>$attrs);

# This uses the count method of $results to return the number of records
matching the filter my $count = $results->count;



#message ("Records returned $count");



#Creates an output file named emailList.csv or returns error.
my $outputfile = IO::File->new("emailList.csv","w") or die "Could not open
output file $!";

#Loop to write each result line to the output file for (my
$i=0;$i<$count;$i++){
	
	#Assigns the result at $i to the variable $foo
	my $foo = $results->entry($i);
		#Assigns the attribute names to variables
		my $sn="sn";
		my $gn="givenName";
		my $mail="mail";
		
		#Assigns the value from $foo to a specific variable
		my $lastname = $foo->get_value($sn);
		my $firsttname = $foo->get_value($gn);
		my $email = $foo->get_value($mail);
		
		#Concatenates the variables to a csv output line.  The \n is
the new line operator
		my $line = "$firsttname,$lastname,$email\n";
		
#  Writes the same output to the screen		
#		message ("$line");

		#Writes the data line to the output file
		$outputfile->print($line);

}

#Closes the output file
undef $outputfile;

#Closes the LDAP connection
$netad->unbind;

#Email the output file to the SharePoint doc library using the SMTP server
Email::Stuff->To('destination e-mail')
			->from('source e-mail')
			->Subject("Automatically generated e-mail list")
			->text_body("E-mail list for Communications
department.  Used for Constant Contact service.")
			->attach_file('emailList.csv')
			->using('SMTP',Host => 'my mail server')
			->send;


sub message
{
    my $m = shift or return;
    print("$m\n");
}
_______________________________________________
Omaha-pm mailing list
Omaha-pm@pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm