in reply to Get the out put when run script through remotely and locally


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);
$t4++;
$td = (1900 + $t5) . ((length($t4)==1)?(0 . $t4):$t4) . ((length($t3)>1)?$t3:(0 . $t3)) . ((length($t2)==1)?(0 . $t2):$t2) . ((length($t1)>1)?$t1:(0 . $t1)) . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
$tds = (1900 + $t5) . ((length($t4)==1)?(0 . $t4):$t4) . ((length($t3)>1)?$t3:(0 . $t3));

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;
$t4++;
$td = 1900 + $t5 . "/" . ((length($t4)==1)?(0 . $t4):$t4) . "/" . ((length($t3)>1)?$t3:(0 . $t3)) . " " . ((length($t2)==1)?(0 . $t2):$t2) . ":" . ((length($t1)>1)?$t1:(0 . $t1)) . ":" . ((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);

$t4++;
my $runtime = 1900 + $t5 . "-" . ((length($t4)==1)?(0 . $t4):$t4) . "-" . ((length($t3)>1)?$t3:(0 . $t3)) . " " . ((length($t2)==1)?(0 . $t2):$t2) . ":" . ((length($t1)>1)?$t1:(0 . $t1)) . ":" . ((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);
}
  • Comment on Re: Get the out put when run script through remotely and locally