6 Replies - 4500 Views - Last Post: 20 February 2008 - 07:26 PM

#1 domquem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 06

perl ftp get file once,push x times

Posted 15 February 2008 - 01:34 AM

hello all[font=Georgia]

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.

Is This A Good Question/Topic? 0
  • +

Replies To: perl ftp get file once,push x times

#2 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: perl ftp get file once,push x times

Posted 15 February 2008 - 08:27 AM

Take a look at Net::FTP.
Was This Post Helpful? 0
  • +
  • -

#3 domquem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 06

Re: perl ftp get file once,push x times

Posted 15 February 2008 - 02:00 PM

this is my code so far::

#!/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_hour:$real_min:$real_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;
Was This Post Helpful? 0
  • +
  • -

#4 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: perl ftp get file once,push x times

Posted 15 February 2008 - 07:42 PM

Okay.
Was This Post Helpful? 0
  • +
  • -

#5 domquem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 06

Re: perl ftp get file once,push x times

Posted 20 February 2008 - 10:26 AM

View Postgirasquid, on 15 Feb, 2008 - 07:42 PM, said:

Okay.



i need a little help on this same script....using FTP i want to send any file that the program transfers remotely successfully to a folder called "SENT".

so my program reads from an OUTPUT folder then moves all the transfered ones to SENT.

please assist. many thanks
Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: perl ftp get file once,push x times

Posted 20 February 2008 - 06:44 PM

Where did you get that code from? If you wrote that code you could figure out how to make it work.
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: perl ftp get file once,push x times

Posted 20 February 2008 - 07:26 PM

:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1