Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Seeking tutelage

by Gnuser (Novice)
on Mar 08, 2002 at 16:27 UTC ( [id://150356]=perlquestion: print w/replies, xml ) Need Help??

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

I'm not very bright. I'm not even a programmer. But I met Perl and the world changed. I want to be a code junkie. And I think I'm eccentric enough to be a Perl monger some day. If the monks knew that I've tried to do my homework, for weeks, and I'm going through every O'Reilly book I can afford, would they please shed light on these tasks I've set for myself.

Problem1: An NT Server on the LAN finishes batch and stores a text log file locally every Saturday. I come to work to retrieve the file and forward it as an attachment to my clients. (Don't get me wrong, I'm glad to have work!)

Using a win32 interface, I was able to send email using mail::sendmail, which displays the date and time in the message, but I don't know how attachments works. (Base, I'm sure, but it took me about three days.) I was able to use net::ftp to log on and put files to an NT server, but I have no idea how to code for the directory location. And I have no idea how to get (though I imagine it's just the opposite of put).

I'm wondering if it's possible to subroutine some ftp code in the same mail Perl script to get the LAN server's log text file. Once done, how do I get the mail subroutine to identify and attach it. Ideally, I'd like to splash the contents of the file into the mail message (text file is very small, and can be parsed if needed). Over the top: Be able to send this message automatically to all my clients (another file?), with a subject line that changes with each client. The showstopper is how to schedule this to kick off every Saturday. I'm finding some utils for NT scheduler on a client, but will have to learn them first.

Problem2: My clients send updates for their servers and often in the form of .zip. Is it possible to push this file (HUGE!) to multiple servers and unzip it? I am learning how to use Net::Gnutella. I realize that a different compression might be needed, so I could use the pack and unpack commands. Over the top: Be able to have the script query me for either the server DNS names or the ip address, then it would go push and complete and tell me things are copacetic.

I apologize initially for my lack of knowledge. I've scoured the many forums and apologize if I missed something in this endless resource that could help. I would appreciate any insight and will gladly research more, learn further, humble monks and even trek to the Gorge of Eternal Peril (any idea what questions three?).

Many, many thanks,
Gnuser
(sorry for the pun)

Replies are listed 'Best First'.
Re: Seeking tutelage
by steves (Curate) on Mar 08, 2002 at 17:17 UTC

    For Problem #1, take a look at Mail::Sender. It has nice attachment support. Here's a script I wrote for some people here about two years ago that's gotten lots of mileage. I've X'ed out one or two things that are ours, but they're fine points that would be good exercises.

    #!/usr/local/bin/perl =head1 NAME mailfiles - General purpose command line mailer supporting attachments =head1 SYNOPSIS B<mailfiles -f from -t to [-du] [-c cc] [-h smtp_host] [-m message] [- +r reply_to ] [-s subject] [file ...]> =head1 REQUIRED OPTIONS =over 4 =item -f from Specifies the from email address as I<from>. =item -t to Send the mail to I<to>, where I<to> is one or more email addresses. M +ultiple addresses should be comma separated and quoted if spaces or special sh +ell characters appear in the addresses. =back =head1 OPTIONAL ARGUMENTS =over 4 =item -c cc Copy one or more email addresses specified by I<cc>. Multiple email a +ddresses must be comma separated and need to be quoted if spaces or special she +ll character appear in them. =item -d Convert all specified file attachments to DOS I<CR/LF> format before attaching. Note that the attached files are modified if this option i +s specified. This is only meaninful if the attached files are text f +iles in UNIX format and the message is being sent to a DOS/Windows client. =item -h smpt_host Use I<smpt_host> as the mail server. Defaults to the B<GSI> corporate + mail server if not specified. =item -m message Put the text specified in I<message> in the sent message body. Needs +to be sent quoted from the shell if whitespace or special characters appear +in it. =item -r reply_to Set the reply to address to I<reply_to>. =item -s subject Set the message subject to I<subject>. =item -u Convert all specified file attachments to UNIX newline format before attaching. Note that the attached files are modified if this option i +s specified. This is only meaninful if the attached files are text file +s in DOS/Windows format and the message is being sent to a UNIX client. =item file ... File(s) to be sent as attachments. =back =head1 AUTHOR Me =cut use strict; use Mail::Sender; use Getopt::Std; use XXX::File::TextConvert; # DOS/UNIX file conversions use vars qw/$opt_c $opt_d $opt_f $opt_h $opt_m $opt_r $opt_s $opt_t $o +pt_u/; my $mail_opts = { smtp => 'XXX.XXX.XXX.XXX', msg => ' ' }; my $message; my $from; my $to; my $cc; my $subject; sub parse_opts { getopts("c:df:h:m:r:s:t:u"); die "Required -f (from) value missing.\n" if (!defined($opt_f)); die "Required -t (to) value missing.\n" if (!defined($opt_t)); die "Only one of -d or -u makes sense.\n" if ($opt_d && $opt_u); $mail_opts->{from} = $opt_f if ($opt_f); $mail_opts->{smtp} = $opt_h if ($opt_h); $mail_opts->{to} = $opt_t if ($opt_t); $mail_opts->{subject} = $opt_s if ($opt_s); $mail_opts->{msg} = $opt_m if ($opt_m); $mail_opts->{cc} = $opt_c if ($opt_c); $mail_opts->{replyto} = $opt_r if ($opt_r); if (scalar(@ARGV) > 0) { unix_to_dos(@ARGV) if ($opt_d); # You write this dos_to_unix(@ARGV) if ($opt_u); # You write this $mail_opts->{file} = join(',', @ARGV); } } sub send_mail { my $sender = new Mail::Sender() or die "Can't create mail sender object: $!\n"; defined($mail_opts->{file}) ? $sender->MailFile($mail_opts) : $sender->MailMsg($mail_opts); } parse_opts(); send_mail(); exit 0;
(RhetTbull) Re: Seeking tutelage
by RhetTbull (Curate) on Mar 08, 2002 at 17:51 UTC
    Is it possible? Most certainly -- everything you asked about is possible and in most cases, rather easy. I recommend reading up on the following modules (most of which can probably be installed using the "ppm" command if you're using ActiveState perl as I suspect you are, or using the cpan shell if you're on a unix system ("perl -MCPAN -e shell"). To send email, I prefer the MIME::Lite module which makes it nice and easy (including sending attachments). Here's some sample code that illustrates using MIME::Lite to send a log file as a message body (this is ripped from some code I use) and sends it.
    use warnings; use strict; use MIME::Lite; my $body; { #snarf the file into the variable $body local $/ = undef; open SCI_DUMP_OUT, "<$sci_dump_out" or die "could not open file $s +ci_dump_out: $! ; $body = <SCI_DUMP_OUT>; close SCI_DUMP_OUT or warn "could not close file $sci_dump_out: $! +"; } #you must define $to, $cc, $subject, $from, and $smtp_server my %msg = ( to => $to, cc => $cc, subject => $subject, body => $body, from => $from, server => $smtp_server); send_email(%msg); sub send_email { #send email via SMTP using MIME::Lite my %email = @_; my @must_have = qw(server from to subject body); foreach (@must_have) {die "send_email: must provide parameter: '$_ +'" if not defined $email{$_}}; MIME::Lite->send('smtp',$email{server},Timeout=>60); my $msg = MIME::Lite->new( From => $email{from}, To => $email{to}, Cc => $email{cc}, Subject => $email{subject}, Type => 'multipart/mixed' ); $msg->attach(Type => 'TEXT', Data => $email{body}, ); $msg->send; }
    You're on the right track using Net::FTP for FTP access. Here's some sample code that gets a remote file:
    use strict; use warnings; use Net::FTP; my ($host, $user, $pass) = qw(host.some.domain username password); my $ftp = Net::FTP->new($host) || die "Could not open connection to '$ +host'"; $ftp->login($user,$pass) or die "could not login: $!"; $ftp->cwd("test"); $ftp->get("that.file"); $ftp->quit();
    Read the Net::FTP documentation for more detail.

    For problem 2, you could also use Net::FTP to push the file to the servers (assuming they have FTP servers installed and running). One approach would be to use a script to push (via FTP) the files to the servers. Have another script that runs on the servers on a scheduled basis and when it sees new files in your incoming directory, it unzips them and processes them. See Archive::Zip for dealing with .ZIP files as well as IO::Zlib for dealing with .gz files.

    As for scheduling things on NT, you can use the "at" command or try the task scheduler. Or if you're running Windows 2000, you can use the Scheduled Tasks service via the Control Panel. To schedule a perl script, I usually use the full path the perl program and the script name as the command for task to run to make sure everything finds the right path. For example, to schedule the task "foo.pl" to run, I set the command to run (in the Task Scheduler or Scheduled Tasks setup) to be something like "c:\Perl\bin\perl.exe c:\foo\foo.pl")

    You might also like to read Dave Roth's "Win32 Perl Scripting: The Administrator's Handbook" for more details about how to use Perl to do admin tasks on Windows.

      Props to RhetTBull. MIME::Lite is the ticket.

      Spend some time learning about MIME types and pay close attention to the mail reader being used by your clients. AOL can't handle things like multipart/alternative mails. Yahoo isn't too good at it either. Some really paranoid mail readers such as SQWebmail won't let you view HTML mails. So if possible, keep your life simple and use plain old text for automated mails that are going out to the world. Eat chicken every day. I like chicken.
Re: Seeking tutelage
by dash2 (Hermit) on Mar 08, 2002 at 17:41 UTC
    Problem 2: skip Net::Gnutella, I doubt if your clients want their updates d1str1bu7ed to hundreds of kiddiez, nor do the kiddiez want your files. Be nice to both parties.

    You certainly could do the pushing to multiple servers. But there's no point using pack, which isn't optimised for file compression. If you need to compress it further, you could tar.gz it (you probably want to unzip it first, the .tar.gz compression will prolly work better on an uncompressed initial file.)

    You can do this from the command line (system 'tar zcvf filename files', system 'unzip filename') or using Archive::Tar and Archive::Zip. But note that the code to unzip the file needs to be on the other side. There's no way to do it remotely (without being rather insecure). So e.g., create a simple script to unzip the files, which runs on a crontab. (Make sure the script can't be fooled into unzipping things into bad places.)

    You might find it easier to unzip first, then upload the raw files. After all, if it is automated, it doesn't matter if it takes longer.

    The last bit, getting it to do it automatically, is the easiest.

    while (<>) { chomp; # can't remember if this is necessary or not my $domain = $_; &send_file_to($domain) or warn "Couldn't send file to $_"; # your sub +routine }

    dave hj~

Re: Seeking tutelage
by Marza (Vicar) on Mar 08, 2002 at 20:48 UTC
    The code examples given are good so I won't offer any. But I would suggest looking into: Beginning Perl by Simon Cozens It is very good for the basics and touches many areas. The Roth books are good for the win32 platforms. I have both myself! ;-) Good luck and enjoy!

      I can't thank everyone enough. I can't believe so many people with powerful knowledge are willing to donate their time and thoughts. The repsonses have given me plenty of meat, even chicken, to chew on. I'm adding to my library also. Many thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://150356]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-24 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found