This is a sample program, to log the output and notify if required.
# Modules used
use Net::FTP;
use File::Copy;
use Net::FTP;
use Net::SMTP;
sub notify($);
my $error,$ftp;
my @t = localtime(time);
my ($rf, $td);
$t
4++;
$td = (1900 + $t
5) . ((length($t
4)==1)?(0 . $t
4):$t
4) . ((length($t
3)>1)?$t
3:(0 . $t
3)) . ((length($t
2)==1)?(0 . $t
2):$t
2) . ((length($t
1)>1)?$t
1:(0 . $t
1)) . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
$tds = (1900 + $t
5) . ((length($t
4)==1)?(0 . $t
4):$t
4) . ((length($t
3)>1)?$t
3:(0 . $t
3));
open(LOG,">>/usr/perl_scripts/get_filenames.log");
mark_time("$tds");
mark_time("1.Connection to the test server");
$ftp = Net::FTP->new("testserver.vago.com", Timeout => 75, Debug => 0, BlockSize => 1024);
if (!$ftp) {
return(notify("Could not connect to testserver.vago.com"));
}
else
{
print LOG "Successfull Connection to the server\n";
}
mark_time("2.Login to the testserver.vago.com");
$ftp->login("testftp","123qwe") || (return(notify("Could not login to testserver.vago.com")));
print LOG "Successfull login to the server\n";
mark_time("3.Get files from the server and then delete them");
$ftp->cwd("/home/testftp/cat/rat");
@dirlist = $ftp->ls();
foreach $file (@dirlist) {
$ftp->get($file,"/home/testftp/cat/rat/$file") || (return(notify("Could not get the files")));
notify("Got the file $file from server to the local machine\n");
print LOG "$tds $file\n";
$ftp->delete($file) || (return(notify("Could not delete $file in server")));
}
$a= "--------------------------------------------------------";
open(PLOG, ">>//usr/perl_scripts/process.log");
printf PLOG ("%-20s%20s\n", $a);
close(LOG);
######################################################################################
# Send emails
sub notify ($) {
my $error = shift;
my $smtp = Net::SMTP->new('mail.vago.com');
if (!$smtp) {
my @t = localtime(time);
my $td;
$t
4++;
$td = 1900 + $t
5 . "/" . ((length($t
4)==1)?(0 . $t
4):$t
4) . "/" .
((length($t
3)>1)?$t
3:(0 . $t
3)) . " " . ((length($t
2)==1)?(0 . $t
2):$t
2) . ":" .
((length($t
1)>1)?$t
1:(0 . $t
1)) . ":" . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
print OUT "
test.PL Could not connect to SMTP server mail.vago.com at $td\n";
close(OUT);
return(undef);
}
my $email = "test.pl: $error\n";
my $MailFrom = "test_test\@vago.com";
$smtp->mail( $MailFrom );
$smtp->recipient("sago.gara\@vago.com", { SkipBad => 1 });
$smtp->data();
$smtp->datasend("Subject: Test: FTP \n");
$smtp->datasend("\n");
$smtp->datasend( $email );
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;
return($error)
}
####################################################################################################
# print out time stamp and message
sub mark_time ($) {
my $message = shift;
my @t = localtime(time);
$t
4++;
my $runtime = 1900 + $t
5 . "-" . ((length($t
4)==1)?(0 . $t
4):$t
4) . "-" . ((length($t
3)>1)?$t
3:(0 . $t
3)) . " " . ((length($t
2)==1)?(0 . $t
2):$t
2) . ":" . ((length($t
1)>1)?$t
1:(0 . $t
1)) . ":" . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
open(PLOG, ">>/usr/perl_scripts/process.log") or warn("can't open log file: $!");
printf PLOG ("%-20s%20s\n", $message, $runtime);
close(PLOG);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.