View Single Post
Old Feb 15th, 2008, 2:57 PM   #2
domquemo
Newbie
 
Join Date: Jan 2006
Posts: 3
Rep Power: 0 domquemo is on a distinguished road
Re: perl ftp get file once,push x times

please help - this is the script so far! Many thanks!

#!/usr/bin/perl

#use strict;
use Net::FTP;
use File::Listing qw(parse_dir);
use File::Glob':glob';
use Cwd;
use Shell;
require 'tickets.cfg';

#print "Content-type:text/html\n\n";

($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime();

$logdate=localtime();

$real_month=$mon+1;
$real_day=$mday;
$real_year=$year+1900;
$real_hour=$hour;
$real_min=$min;
$real_sec=$sec;

#### APPEND THE "O" BEFORE VALUES WHEN WE ARE NOT PAST 1200HRS

if ($real_month<10) {$real_month="0$real_month"}
if ($real_day<10) {$real_day="0$real_day"}
if ($real_min<10) {$real_min="0$real_min"}
if ($real_sec<10) {$real_sec="0$real_sec"}
if ($real_hour<10) {$real_hour="0$real_hour"}

$now = "$real_day/$real_month/$real_year/$real_hourreal_minreal_sec";
$now2 = "$real_day-$real_month-$real_year";

$logfile = $now2.".cdr_transfer.log";

#change local directory
#chdir($billing_server_path) or die "Sorry, cannot change local directory!\n";
#print "\n".$billing_server_path."\n";

open (LOG, ">>$logfile") or die "\nError opening $logfile: $!";
print LOG "\n$now : ******************LOG ALL ACTIVITY*****************\n";


# CONNECT TO .182 & GET READY TO PICK FILES

if ($ftp = Net::FTP->new($ftp_from_ip, Timeout => 20))
{
print "\n** Connecting to server $ftp_from_ip.....OK!\n";
print LOG "$now --- Connecting to server $ftp_from_ip.....Ok!\n";}
else {
print "\nError Connecting $ftp_from_ip. Network or Server Problem.\n", $ftp->message;
print LOG "$now --- Error Connecting $ftp_from_ip. Network or Server Problem.\n", $ftp->message;exit;}

if ($ftp->login($ftp_from_user_id,$ftp_from_user_pw)) {
print "** Username and Password AUTHENTICATED.....Login OK!\n";
print LOG "$now --- Username and Password AUTHENTICATED.....Login Ok!\n";}
else {
print "Invalid User/Pass! ", $ftp->message;
print LOG "$now --- Invalid User/Pass! ", $ftp->message; exit;}

#if ($ftp->binary) { print "FTP Cannot do binary\n";exit;}

if($ftp->cwd($ftp_from_source_dir)) {
print "** Directory Changed to $ftp_from_source_dir.....OK!\n";
print LOG "$now --- Directory Changed to $ftp_from_source_dir.....Ok!\n";}

else {
print"Source Dir. Error: $ftp_from_source_dir. ", $ftp->message;
print LOG "$now --- Source Dir. Error: $ftp_from_source_dir. ", $ftp->message;exit;}

# CONNECT TO .183 & GET READY TO DUMP FILES

if ($ftp1 = Net::FTP->new($ftp_to_ip, Timeout => 60)) {
print "\n** Connecting to server $ftp_to_ip.....OK!\n";
print LOG "$now --- Connecting to server $ftp_to_ip.....OK!\n";}

else {
print "\nError Connecting to $ftp_to_ip. Network or Server Problem.\n";
print LOG "$now --- Error Connecting $ftp_to_ip. Network or Server Problem.\n", $ftp->message;exit;}

if ($ftp1->login($ftp_to_user_id,$ftp_to_user_pw)) {
print "** Username and Password AUTHENTICATED.....Login OK!\n";
print LOG "$now --- Username and Password AUTHENTICATED.....Login Ok!\n";}

else {
print "Invalid User/Pass! ", $ftp->message;
print LOG "$now --- Invalid User/Pass! ", $ftp->message; exit;}

#if ($ftp1->binary) { print "FTP Cannot do binary\n";exit;}

if($ftp1->cwd($ftp_to_target_dir)) {
print "** Directory Changed to $ftp_to_target_dir.....OK!\n";
print LOG "$now --- Directory Changed to $ftp_to_target_dir.....Ok!\n";}
else {
print"Target Dir. Error: $ftp_to_target_dir. ", $ftp->message;
print LOG "$now --- Source Dir. Error: $ftp_to_target_dir. ", $ftp->message;exit;}

print "\nChanged directory to ".$ftp->pwd()."\n\n";
print LOG "$now --- Changed directory to ".$ftp->pwd()."\n";


# for each new file copy locally and also send to MIS STAGING
# Retrieve a recursive directory listing
#my @ls = $ftp->ls('-lR');

my @ls = $ftp->ls('-l');

#print "\nretrieving... @ls\n";

my $file;

foreach $file (parse_dir(\@ls))
{
my($name, $type, $size, $mtime, $mode) = @$file;

#print "\n$name- $type - $size - $mtime - $mode\n";
# We only want to process plain files,
# we shall ignore symbolic links

next unless $type eq 'f';

## file size should be more than 0
if ($size == 0) {
print"Invalid file Size. Abort file tranfer!\n";
print LOG "$now --- Invalid file Size.....Application exiting!\n";exit;}

####target space limited

######file not found

# Get the file from the ftp server
print "\n##########File Transfer Start##########\n";
print LOG "$now --- ##########File Transfer Start##########\n";

print "\n-- Getting $name from Server_182\n";
print LOG "$now --- Getting $name from Server_182...Ok!\n";

#$ftp->get($name) or die "Couldn't get from Server_182 '$name', skipping.. $!\n\n";


if ($ftp->get($name)) {
#print "Success\n";
#print LOG "$now - Success\n";
}
else{print "Couldn't get from Server_182 '$name', skipping.. $!\n";
print LOG "$now --- Couldn't get from Server_182 '$name', skipping.. $!\n"}

if (-e $name)
{
print" - File exist...processing!\n";
print LOG "$now --- File [$name] exist...Processing!\n";}
else {print " - File does not exist!!\n";
print LOG "$now --- File [$name] does not exist!\n";exit;
}


# push the file to 183
print "++ Pushing $name to Server_183\n";
print LOG "$now --- Pushing $name to Server_183...Ok!\n";


if ($ftp1->put($name)) {
#print "Success\n";
#print LOG "$now - Success\n";
}
else{print "Couldn't get from Server_183 '$name', skipping.. $!\n";
print LOG "$now --- Couldn't get from Server_183 '$name', skipping.. $!\n"}


#$ftp1->put($name) or warn "Couldn't push to Server_183 '$name', skipping.. $!\n\n";

# backup the original file
#print "Renaming $name from 183...";
#$ftp->rename($name,$ftp_in_bk_dir.$name) or warn "Couldn't get '$name', skipped: $!\n";

#print"\nFiles Pushed to $ftp_to_target_dir Successfully!\n";

}

print"\nAll file transfer Ok!\n";
print LOG "$now --- All file transfer Ok!\n";
print LOG "$now : ******************END OF ACTIVITY*****************\n";
# close FTP connections. we are done
close LOG;

$ftp->quit;
$ftp1->quit;
domquemo is offline   Reply With Quote