#!/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 $ftpuser = "aseftp"; my $ftpuser1 = "asega"; my $recvOKstr = "OK UPLOAD"; my $ext = ".csv"; my $hostname = "160.78.21.55"; my $username = 'asega'; my $password = 'mums'; GetOptions( "file=s" => \$readingFile ) or die("Error in command line arguments\n"); print getTimeStamp("log") . " >> $readingFile \n"; my($scriptname, $scriptpath) = fileparse($0); my($filename, $path, $suffix) = fileparse($readingFile, qr/\.[^.]*/); my $send = $readingFile =~ s/received/transmitted/r; my($tool, $trxdate) = split(/_/,$filename); my($dmy1, $dmy2, $dmy3, $unit, $dmy4) = split(/\//,$path); print getTimeStamp("log") . " >> Unit $unit - Filename $tool" . "$suffix \n"; my $ftp = Net::FTP->new($hostname, Timeout => 20, Debug => 0, Passive => 0) or warn getTimeStamp("log") . " >> Cannot connect to $hostname: $@"; if ($ftp->login($username,$password)) { if ($ftp->put($readingFile, $tool . $suffix)) { print getTimeStamp("log") . " >> Put $readingFile completed.\n"; if ( !move $readingFile, $send) { warn getTimeStamp("log") . " >> Move $readingFile -> $send failed: $!"; } else { print getTimeStamp("log") . " >> Moved $readingFile -> $send.\n"; } } else { warn getTimeStamp("log") . " >> Put $readingFile failed ", $ftp->message; } } else { warn getTimeStamp("log") . " >> Cannot login ", $ftp->message; } exit(0);