Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have requirement of creating a perl script, that can do a ldap search and output the content to csv format, then compress and encrypt and then ftp to the destinations.

Is there anyone who can guide me with this?.

Thanks, Pamela

Replies are listed 'Best First'.
Re: Encrypt File while sending
by Your Mother (Archbishop) on Aug 20, 2009 at 18:09 UTC

    This can certainly be done and you can get help here but you've sort of dropped a largish requirements list in a sentence. You'll get great help if you ask specific questions or show code you've tried that isn't doing what you'd like.

    For your requirements, search in the CPAN for the keywords you're after LDAP, CSV, Encrypt, and FTP. You'll find plenty of places to start hacking or issues about which you can ask specific questions here. Questions showing code/effort tend to get better answers.

Re: Encrypt File while sending
by Utilitarian (Vicar) on Aug 20, 2009 at 18:16 UTC
    • You can perform the search using Net:LDAP
    • You can generate a csv from the resulting data using Text::CSV
    • You haven't specified an encryption algorithm, so try Crypt::Simple
    • You can FTP to/from a site using Net::FTP
    • Finally you can use CPAN
    Have a look at the above, make an effort to write a solution and if you have difficulties, come back to us explaining what the issue is with a sample of code displaying your issue. Best of luck
      Hi, I have the code with me. I don't know to move forward with encryption part of it and the security folks will not allow plain ftp, it has to be "sftp". To step down further, i'm looking for steps where it does the encryption and sftp. Here is the code, which will do a ldap search and convert the output to a csv format :
      #!/usr/bin/perl use Net::LDAP; @fields = # fields to expo +rt ( # Label Field Name Index ['UID','uid',0], ['mail','mail',0], ['OtherPhone','OtherPhone',0], ['mobile','mobile',0], ['givenName','givenName',0], ['Initials','initials',0], ['sn','sn',0], ['c','c',0], ); $ldap = Net::LDAP->new('hostname:port') or die "$@"; # open conne +ction $mesg = $ldap->bind( # login "cn=Directory Manager", password => "$$$$$$$$$$$"); $mesg = $ldap->search( # search base => "dc=xyz,dc=com", filter => "(&(objectClass=*)(!(c=US))(uid=*))" ); $DATETIME=`date`; sub get # get field valu +e { return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : ''; } print "$DATETIME"; # first line with file created date/ +time print '"'.(join '","', "Report", # header map {$_->[0]} @fields)."\"\n"; foreach $entry ($mesg->all_entries) # for each entry { @dn = map {s/[a-z]+=//gi; $_ = ucfirst} # organization reverse split /,/, $entry->dn; shift @dn; shift @dn; shift @dn; pop @dn; print '"'.(join '","', "@dn", # row map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; } $mesg = $ldap->unbind; # close connecti +on
      I run the command like this : perl report.pl > report.csv Thanks, Pamela
        • Print to a file within the script rather than in the shell, this will allow you to automate the entire process.
        • Read the documentation of Crypt::Simple, it's fairly straightforward.
        • Substitute Net::SFTP for Net::FTP in my previous advice.
        • Seriously, you can search for things in CPAN, it's easy to do and speeds up development no end ;)