#!/usr/bin/perl -w use strict; use Net::FTP; use Cwd; $|++; my ($ftpmessage, $ftp, $sourcefile, $option, $remote_path); my ($pwdr, $logfile); my %parm = ( ftphost => '255.255.255.255', ftpuser => 'user', ftppass => 'passwd', ); sub get_the_file($) { my $source=shift; $ftp->get("$source") or LogIt('exit', "Error uploading.\nDisk space or permissions problems?\n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } sub get_the_pwd { my $fullpath=$ftp->pwd(); print "Fullpathname $fullpath\n"; $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } sub set_the_pwd($) { my $directory=shift; $ftp->cwd("$directory") or LogIt('exit', "Could not change the directory to $directory! \n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } # Print message with date+timestamp to logfile. # Abort program if instructed to. sub LogIt { my $exit = $_[0]; my $msg = $_[1]; print LOG "$msg"; #my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); print LOG scalar localtime(), "\n"; #$year+1900,"-",$mon+1,"$mday $hour:$min:$sec\n"; exit if ($exit eq 'exit' ); } { $pwdr=cwd;; chomp($pwdr); if ( $#ARGV == -1 ) { print "$0:Usage $0 get\|put\|list path filename\n"; exit; } elsif ($ARGV[0] !~ /(get)|(put)|(list)/i) { print "$0:Usage $0 get\|put\|list path filename\n"; exit; } $option=$ARGV[0]; $remote_path=$ARGV[1]; $sourcefile=$ARGV[2]; if ( -e "$pwdr\\FTPLogs") { print "Writing Log file in $pwdr\\FTPLogs...\n"; } else { mkdir("$pwdr\\FTPLogs",0777) || die "Sorry, I couldn't create $pwdr\\FTPLogs folder $!\n"; } ($main::sec,$main::min,$main::hour,$main::mday,$main::mon,$main::year,,,) = localtime(time); $main::mon+=1; $main::year+=1900; $logfile="$pwdr\\FTPLogs\\FTP_log_$main::mday\_$main::mon\_$main::year\_$main::hour\_$main::min\_$main::sec"; open (LOG, ">>$logfile") or die "Error opening $logfile: Reason$!"; LogIt('noexit', "\n\n************************************************************\n Started run\n"); print LOG " Destination server $parm{ftphost} Destination user $parm{ftpuser} Log file $logfile Upload script $0 Perl $] Net::FTP $Net::FTP::VERSION Local OS $^O\n", ; if ((defined $sourcefile) && ($option =~ /put/i)) { unless (-r $sourcefile && -T $sourcefile) { LogIt('exit', "Error with source file $sourcefile.\nIs not readable or ASCII.\n" ); } } $ftp = Net::FTP->new($parm{ftphost}) or LogIt('exit', "Error connecting.\nNetwork or server problem?\n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); $ftp->login($parm{ftpuser}, $parm{ftppass}) or LogIt('exit', "Error logging in.\nHas ID or password changed?\n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); $ftp->ascii(); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); set_the_pwd("$remote_path"); get_the_pwd(); if ($option =~ /put/i) { put_the_file("$sourcefile"); } elsif ($option =~ /get/i) { get_the_file("$sourcefile"); } else { get_file_list(); } $ftp->quit() or LogIt('exit', "Error disconnecting.\n???\­n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); close LOG or die "Error closing $sourcefile: Reason$!"; }