[Omaha.pm] My first script

Theodore Katseres tedkat at gmail.com
Thu Mar 8 10:45:57 PST 2012


Just throwing this out there; If you are querying Active Directory you will
run into
the 1000 results per query limit.  Look into using Net::LDAP::Control::Paged
to help with this.

Here is an example

sub paged_query
{
    my ( $ldap, $args, $page_size ) = @_;
    my ( @entries, $cookie );

    $page_size = 100 if ( !defined $page_size || $page_size < 1 );
    my $page = Net::LDAP::Control::Paged->new( size => $page_size );
    push(@$args, (control => [$page]));

    my $mesg;

    while ( $mesg = $ldap->search(@$args) ) {
        last if ( $mesg->code );
        push(@entries, $e) for my $e ( $mesg->entries );
        last unless ( my ($resp) = $mesg->control(LDAP_CONTROL_PAGED)
);## list context! `my ($resp)`
        last unless ( $cookie = $resp->cookie );
        $page->cookie($cookie);
    }

    warn $mesg->error if ( $mesg->code );

    if ($cookie) {
        $page->cookie($cookie);
        $page->size(0);
        $ldap->search(@$args);
        die 'LDAP Search error';
    }
    return ( \@entries );
}


On Thu, Mar 8, 2012 at 12:05 PM, Bill Brush <bbrush at gmail.com> wrote:

> 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))(givenname=*)))";
>
> # $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 at pm.org
> http://mail.pm.org/mailman/listinfo/omaha-pm
>



-- 
Ted Katseres
      ||=O=||
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/omaha-pm/attachments/20120308/b62863b2/attachment.html>


More information about the Omaha-pm mailing list