Files
ase_perl/mind/UnitCsvRetransmit.pl
2021-03-18 21:40:49 +01:00

65 lines
1.7 KiB
Perl
Executable File

#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use File::SmartTail;
use File::Basename qw( fileparse );
use File::Copy qw( move );
use Net::FTP;
use POSIX;
$SIG{CHLD} = 'IGNORE';
$|++; # Autoflush
sub getTimeStamp
{ # parm [ts] => timestamp for filename; log => timestamp for log
my $format = "%04d%02d%02d%02d%02d%02d";
my ($p1) = @_;
if ( defined $p1 and $p1 eq "log" ) {
$format = "%04d%02d%02d %02d:%02d:%02d";
}
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time);
my $timestamp =
sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
return $timestamp;
}
my $readingFile;
my $hostname = "80.211.60.65";
my $username = 'asega';
my $password = 'mums';
GetOptions( "file=s" => \$readingFile )
or die("Error in command line arguments\n");
(my $file2ASE = $readingFile) =~ s/trx2ASE\///;
print getTimeStamp("log") . " >> $file2ASE \n";
my ( $filename, $path, $suffix ) = fileparse( $file2ASE, qr/\.[^.]*/ );
my ( $tool, $trxdate ) = split( /_/, $filename );
my @dirs = split( /\//, $path );
print getTimeStamp("log") . " >> Unit $dirs[-1] - Filename $tool" . "$suffix \n";
my $ftp = Net::FTP->new( $hostname, Timeout => 20, Debug => 0, Passive => 1 )
or die getTimeStamp("log") . " >> Cannot connect to $hostname: $@";
if ( $ftp->login( $username, $password ) ) {
if ( $ftp->put( $file2ASE, $tool . $suffix ) ) {
print getTimeStamp("log") . " >> Put $file2ASE completed.\n";
if (-e $readingFile) {
unlink $readingFile ;
}
}
else {
die getTimeStamp("log") . " >> Put $file2ASE failed ", $ftp->message;
}
}
else {
die getTimeStamp("log") . " >> Cannot login ", $ftp->message;
}
exit(0);