Hi! I have a perl script that renames and ftp files to another server dynamically(whenever a file comes in the script would automatically kick off).
However from time to time ftp connection would get hosed up so if ftp connection fails I have the perl script ot place the failed file in a tmp folder and would have to manually edit the perl script by specifically naming the file
($instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin'
I needed to be rename/ftp to the specific server. This can get a bit tedius for if 15 files failed then i would have to edit the perl script to the specific filename and run the perl script and repeat.
I would like to automate this process. But I am having the problems of wildcard or capturing the file. Below is the script i am currently using for the manual process. Thanks in advance
==========================================================
#!/usr/bin/perl
use File::Copy;
use File::Basename;
my $now=localtime;
open(LOG, ">>/usr/local/cyclone/logs/GXS_IMFP.log");
open(REPORT, ">>/usr/local/cyclone/logs/IMFP_RENAME.log");
print LOG "$now->GXS_IMFP.pl script STARTED!\n";
#FILE NAMES DEFINED
$temp_path = '/usr/local/cyclone/data/ZZAAFES/old/tmp';
$archive_path= '/usr/local/cyclone/data/ZZAAFES/old/';
#$instream_filename = $ARGV[0];
$instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin';
$cyclone_filename = $instream_filename;
#UNCOMMENT THE ONE BELOW
$instream_short_filename = basename($instream_filename);
$temp_filename = $temp_path.$instream_short_filename;
$archive_filename = $archive_path.$instream_short_filename;
my ($sec,$min,$hours,$mday,$month,$year,$wday,$yday,$isdst)=localtime();
$year+=1900;
$month+=1;
if ($month < 10) {
$month="0".$month;
}
if ($mday < 10) {
$mday="0".$mday;
}
if($hours < 10) {
$hours="0".$hours;
}
if($min < 10) {
$min="0".$min;
}
if($sec < 10) {
$sec="0".$sec;
}
$endpoint = length ($instream_short_filename);
$short_filename = substr($instream_short_filename, 0, $endpoint -9);
$newfilename= "gxs.tmp";
#$retekfname="gxs_$year$month$mday.$hours$min$sec.1";
$retekfname="gxs_" . $year . $month . $mday . "_" . $hours . $min . $sec . "_" . $short_filename . ".1";
#FTP ROUTINE CALLED
$ftpsub_rc=0;
$ftpsub_rc=ftpsub($cyclone_filename);
if ($ftpsub_rc == 0) {
print LOG "FILE FOUND\n";
}
#CHECK TO SEE IF INITIAL FTP FAILED, WAITS 5 MINS AND ATTEMPTS TO RESTART FTP
if ($ftpsub_rc ==1) {
print LOG "FTP failed, attempting to send again\n";
sleep(300);
$ftpsub_rc=ftpsub($cyclone_filename);
}
#CHECK TO SEE IF FTP RESTART FAILED AND SEND EMAIL NOTIFICATION
if ($ftpsub_rc == 1) {
print LOG "FTP failed 2nd attempt, sending email notification\n";
copy $cyclone_filename, $temp_filename;
$emailsub_rc=emailsub($cyclone_filename);
print LOG "Email subroutine return code is: $emailsub_rc";
}
move $cyclone_filename, $archive_filename;
print LOG "$now->GXS_IMFP.pl script completed\n";
#INITIATES FTP CONNECTION TO server.com AND RENAMES FILE
sub ftpsub {
print LOG "$now->FTP CONNECT TO server.com STARTED\n";
use Net::FTP;
my $ftp;
$ftp = Net::FTP->new("server.com", port =>21) || ((print LOG "$now->ERROR:FTP CONNECT TO server.com FAILED\n"), return(1));
$ftp->login("logid", "pwd") || ((print LOG "$now->ERROR:FTP LOGIN TO server.com FAILED\n"), return(1));
$ftp->put($instream_filename, $newfilename) || ((print LOG "$now->ERROR:FTP PUT TO server.com FAILED\n"), return(1));
$ftp->rename($newfilename, $retekfname) || ((print LOG "$now->ERROR:FTP RENAME TO server.com FAILED\n"),return(1));
$ftp->quit();
print LOG "$now->FTP TO server.com COMPLETED\n";
return (0);
};
print REPORT "$now->$instream_short_filename RENAME $retekfname\n";
However from time to time ftp connection would get hosed up so if ftp connection fails I have the perl script ot place the failed file in a tmp folder and would have to manually edit the perl script by specifically naming the file
($instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin'
I needed to be rename/ftp to the specific server. This can get a bit tedius for if 15 files failed then i would have to edit the perl script to the specific filename and run the perl script and repeat.
I would like to automate this process. But I am having the problems of wildcard or capturing the file. Below is the script i am currently using for the manual process. Thanks in advance
==========================================================
#!/usr/bin/perl
use File::Copy;
use File::Basename;
my $now=localtime;
open(LOG, ">>/usr/local/cyclone/logs/GXS_IMFP.log");
open(REPORT, ">>/usr/local/cyclone/logs/IMFP_RENAME.log");
print LOG "$now->GXS_IMFP.pl script STARTED!\n";
#FILE NAMES DEFINED
$temp_path = '/usr/local/cyclone/data/ZZAAFES/old/tmp';
$archive_path= '/usr/local/cyclone/data/ZZAAFES/old/';
#$instream_filename = $ARGV[0];
$instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin';
$cyclone_filename = $instream_filename;
#UNCOMMENT THE ONE BELOW
$instream_short_filename = basename($instream_filename);
$temp_filename = $temp_path.$instream_short_filename;
$archive_filename = $archive_path.$instream_short_filename;
my ($sec,$min,$hours,$mday,$month,$year,$wday,$yday,$isdst)=localtime();
$year+=1900;
$month+=1;
if ($month < 10) {
$month="0".$month;
}
if ($mday < 10) {
$mday="0".$mday;
}
if($hours < 10) {
$hours="0".$hours;
}
if($min < 10) {
$min="0".$min;
}
if($sec < 10) {
$sec="0".$sec;
}
$endpoint = length ($instream_short_filename);
$short_filename = substr($instream_short_filename, 0, $endpoint -9);
$newfilename= "gxs.tmp";
#$retekfname="gxs_$year$month$mday.$hours$min$sec.1";
$retekfname="gxs_" . $year . $month . $mday . "_" . $hours . $min . $sec . "_" . $short_filename . ".1";
#FTP ROUTINE CALLED
$ftpsub_rc=0;
$ftpsub_rc=ftpsub($cyclone_filename);
if ($ftpsub_rc == 0) {
print LOG "FILE FOUND\n";
}
#CHECK TO SEE IF INITIAL FTP FAILED, WAITS 5 MINS AND ATTEMPTS TO RESTART FTP
if ($ftpsub_rc ==1) {
print LOG "FTP failed, attempting to send again\n";
sleep(300);
$ftpsub_rc=ftpsub($cyclone_filename);
}
#CHECK TO SEE IF FTP RESTART FAILED AND SEND EMAIL NOTIFICATION
if ($ftpsub_rc == 1) {
print LOG "FTP failed 2nd attempt, sending email notification\n";
copy $cyclone_filename, $temp_filename;
$emailsub_rc=emailsub($cyclone_filename);
print LOG "Email subroutine return code is: $emailsub_rc";
}
move $cyclone_filename, $archive_filename;
print LOG "$now->GXS_IMFP.pl script completed\n";
#INITIATES FTP CONNECTION TO server.com AND RENAMES FILE
sub ftpsub {
print LOG "$now->FTP CONNECT TO server.com STARTED\n";
use Net::FTP;
my $ftp;
$ftp = Net::FTP->new("server.com", port =>21) || ((print LOG "$now->ERROR:FTP CONNECT TO server.com FAILED\n"), return(1));
$ftp->login("logid", "pwd") || ((print LOG "$now->ERROR:FTP LOGIN TO server.com FAILED\n"), return(1));
$ftp->put($instream_filename, $newfilename) || ((print LOG "$now->ERROR:FTP PUT TO server.com FAILED\n"), return(1));
$ftp->rename($newfilename, $retekfname) || ((print LOG "$now->ERROR:FTP RENAME TO server.com FAILED\n"),return(1));
$ftp->quit();
print LOG "$now->FTP TO server.com COMPLETED\n";
return (0);
};
print REPORT "$now->$instream_short_filename RENAME $retekfname\n";