#!/usr/bin/perl -w use strict; use warnings; use File::Copy; use Net::FTP; use Net::Netrc; # write the log BEGIN { use CGI::Carp qw(carpout); my $errorlog = "/var/ftp/log/errorlog.txt"; open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n"); print LOG "Errors:\n"; carpout(*LOG); } my $recipient = "adress\@domain.de"; my $linux = "root\@domain.de"; my $server = "X.X.X.X"; my $user = "anonymous"; my $passwd = ""; chdir "/var/ftp" or die "/var/ftp: $!\n"; # DO NOT transfer without info file -f "/var/ftp/info" or die "info file IS MISSING !\n"; # open and read info file @ARGV = ("info"); my @ren; while (<>) { # for every line in info m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX" -f $1 or next; # skip if file to rename does not exist -f $2 and next; # skip if file to rename does not exist push @ren, [ $1, $2 ]; } unless (@ren == 2) { die "files are NOT complete !\n"; # extend to your feeling } # rename files according to info file foreach my $f (@ren) { my ($f1, $f2) = @$f; move ($f1, $f2); # Rename files # ftp transfer my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp or die "$server: cannot connect: $@"; # If you don't use ~/.netrc $ftp->login ($user,$passwd) or die "$_: Could not login: " . $ftp->message; # change remote directory for the first file $ftp->cwd("/DE/IN"); # Put file 2 (not 1) to the ftp server # server might go down, so retry twice foreach my $try (0 .. 2) { $ftp->put ($f2) and last; # Success if ($try == 2) { # send mail when transfer of second file failed open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL "from:$linux\n"; print MAIL "to:$recipient\n"; print MAIL "subject:ftp transfer of second file failed ! !\n"; print MAIL "hi \n\n"; print MAIL "ftp transfer of second file failed !\n\n"; print MAIL "Time: ", scalar localtime, "\n"; close(MAIL); die "$server: cannot put $f2: " . $ftp->message; } print STDERR "Trasfer of $f2 failed: ",$ftp->message, ", retry in 10 minutes\n"; sleep (10 * 60); } $ftp->quit; # And wait 15 minutes sleep (15 * 60); } # read the log file my $content; open(FILE, "/var/ftp/log/errorlog.txt") || die "Cant open file. Reason: $!"; # (-: while() { $content .=$_; # get all of the file into content including past new lines } close(FILE); # send the mail with log contents, when transfer completed open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!"; print MAIL "from:$linux\n"; print MAIL "to:$recipient\n"; print MAIL "subject:transfer was successfully !\n"; print MAIL "hi \n\n"; print MAIL "ftp transfer was successfully ! \n\n"; print MAIL "log file: \n"; print MAIL "$content \n\n"; print MAIL "Time: ", scalar localtime, "\n"; close(MAIL); exit;