Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2008, 2:43 AM   #1
domquemo
Newbie
 
Join Date: Jan 2006
Posts: 3
Rep Power: 0 domquemo is on a distinguished road
perl ftp get file once,push x times

hello all

I need a few tips on how i can accomplish a rather simple tasks but a little confusing.

##The task:
Involves getting numerous files one by one from a source server via ftp and pushing that file into a number of other remote servers...say x servers.
the ftp program will be run from cron every hour.

##The process:

my perl ftp program is required to login to a source server A, go to a specific directory and get all files there...login again, send the files one by one to remote server B, C, D, E.

if the fetch and push is Sucsess in all B, C, D, E delete the file from A.

if either of the push fails in either one of the servers say...server B being down, my perl script needs to NOT delete the file on server A unless the transfer to server B is also Successful.

##ideas:
Im thinking of say - retrying the tranfer after every 30 seconds....but also am seeing a possibility of server being down for hours...

Am not able to figure out in my mind how i can get through this problem, any help is appreciated.
domquemo is offline   Reply With Quote
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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC