Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > loops in perl automated ftp

Reply
Thread Tools

loops in perl automated ftp

 
 
banzai
Guest
Posts: n/a
 
      01-08-2009
Need help with the following automated ftp script please;

if (defined ($ftp1)) {
$ftp1->login($FTP1user,$FTP1password) or LogError("FTP login to
$FTP1server failed");
if (defined $FTP1destination){
$ftp1->cwd($FTP1destination) or LogError("Could not change directory
to $FTP1destination on $FTP1server");
}
$ftp1->binary;
$ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
$FileBodyName.tar.gz to $FTP1server");
$ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
$DataTagName.gz to $FTP1server");
$ftp1->quit;
} else {
LogError("FTP open failed to $FTP1server, error code: $!");
}
#



I need to modify the above so that the ftp transfers (put's) are attempted
up to 5 times in case the remote server does not respond. No time delay
needed between retries.

TIA


 
Reply With Quote
 
 
 
 
Snorik
Guest
Posts: n/a
 
      01-08-2009
On 8 Jan., 11:25, "banzai" <(E-Mail Removed)> wrote:
> Need help with the following automated ftp script please;
>
> if (defined ($ftp1)) {
> $ftp1->login($FTP1user,$FTP1password) or LogError("FTP login to
> $FTP1server failed");
> if (defined $FTP1destination){
> $ftp1->cwd($FTP1destination) or LogError("Could not change directory
> to $FTP1destination on $FTP1server");
> }
> $ftp1->binary;
> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> $FileBodyName.tar.gz to $FTP1server");
> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> $DataTagName.gz to $FTP1server");
> $ftp1->quit;} else {
>
> LogError("FTP open failed to $FTP1server, error code: $!");}
>
> #
>
> I need to modify the above so that the ftp transfers (put's) are attempted
> up to 5 times in case the remote server does not respond. No time delay
> needed between retries.
>
> TIA


Why not simply use a counter variable in a while loop?

my $count = 0;
while ($count <= 4)
{
$ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP
copy $FileBodyName.tar.gz to $FTP1server");
$ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
$DataTagName.gz to $FTP1server");
$count++;
}

?
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      01-08-2009
["Followup-To:" header set to comp.lang.perl.misc.]

Snorik <(E-Mail Removed)> wrote:
> On 8 Jan., 11:25, "banzai" <(E-Mail Removed)> wrote:
>> Need help with the following automated ftp script please;
>>
>> if (defined ($ftp1)) {
>> $ftp1->login($FTP1user,$FTP1password) or LogError("FTP login to
>> $FTP1server failed");
>> if (defined $FTP1destination){
>> $ftp1->cwd($FTP1destination) or LogError("Could not change directory
>> to $FTP1destination on $FTP1server");
>> }
>> $ftp1->binary;
>> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
>> $FileBodyName.tar.gz to $FTP1server");
>> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
>> $DataTagName.gz to $FTP1server");
>> $ftp1->quit;} else {
>>
>> LogError("FTP open failed to $FTP1server, error code: $!");}
>>
>> #
>>
>> I need to modify the above so that the ftp transfers (put's) are attempted
>> up to 5 times in case the remote server does not respond. No time delay

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> needed between retries.
>>
>> TIA

>
> Why not simply use a counter variable in a while loop?



Because if the remote server _does_ respond, it will upload the
same file 5 times when once was enough.

The solution must somewhere include a test of whether the server
responded or not.


> my $count = 0;
> while ($count <= 4)


That is an error-prone way to do it.

foreach my $count ( 0 .. 4 )
or
foreach my $count ( 1 .. 5 )

is much easier to read (and therefore, easier to get right).


> {
> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP
> copy $FileBodyName.tar.gz to $FTP1server");
> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> $DataTagName.gz to $FTP1server");
> $count++;
> }



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      01-08-2009
On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <(E-Mail Removed)> wrote:

b> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
b> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy $DataTagName.gz to $FTP1server");

b> I need to modify the above so that the ftp transfers (put's) are attempted
b> up to 5 times in case the remote server does not respond. No time delay
b> needed between retries.

Each one can be done like so (untested code):

for (1..5)
{
last if $ftp1->put("$FileBodyName.tar.gz");
LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
}

In other words, do the loop 5 times unless one of the put() calls
succeeds. If that happens, exit early with `last'.

Ted
 
Reply With Quote
 
banzai
Guest
Posts: n/a
 
      01-08-2009

"Ted Zlatanov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <(E-Mail Removed)> wrote:
>
> b> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> $FileBodyName.tar.gz to $FTP1server");
> b> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> $DataTagName.gz to $FTP1server");
>
> b> I need to modify the above so that the ftp transfers (put's) are
> attempted
> b> up to 5 times in case the remote server does not respond. No time delay
> b> needed between retries.
>
> Each one can be done like so (untested code):
>
> for (1..5)
> {
> last if $ftp1->put("$FileBodyName.tar.gz");
> LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> }
>
> In other words, do the loop 5 times unless one of the put() calls
> succeeds. If that happens, exit early with `last'.
>
> Ted



Thanks Ted - looks like a good solution so I will try.
Snorik, this one suits best I think as your solution ignored exit status (if
thats the right word in perl) of previous attempt(s) . i.e. if first attempt
succeeds then no need to retry


 
Reply With Quote
 
banzai
Guest
Posts: n/a
 
      01-08-2009

"Ted Zlatanov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <(E-Mail Removed)> wrote:
>
> b> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> $FileBodyName.tar.gz to $FTP1server");
> b> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> $DataTagName.gz to $FTP1server");
>
> b> I need to modify the above so that the ftp transfers (put's) are
> attempted
> b> up to 5 times in case the remote server does not respond. No time delay
> b> needed between retries.
>
> Each one can be done like so (untested code):
>
> for (1..5)
> {
> last if $ftp1->put("$FileBodyName.tar.gz");
> LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> }
>
> In other words, do the loop 5 times unless one of the put() calls
> succeeds. If that happens, exit early with `last'.
>
> Ted



Actually, one more question

Could a smiliar 5x loop be put around the whole ftp sequence i.e. login, cwd
and put so tha the whole process is repeated 5 times if required (not just
the put) and exit early if the put succeeds


 
Reply With Quote
 
Snorik
Guest
Posts: n/a
 
      01-08-2009
On 8 Jan., 16:13, "banzai" <(E-Mail Removed)> wrote:
> "Ted Zlatanov" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <(E-Mail Removed)> wrote:

>
> > b> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> > $FileBodyName.tar.gz to $FTP1server");
> > b> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> > $DataTagName.gz to $FTP1server");

>
> > b> I need to modify the above so that the ftp transfers (put's) are
> > attempted
> > b> up to 5 times in case the remote server does not respond. No time delay
> > b> needed between retries.

>
> > Each one can be done like so (untested code):

>
> > for (1..5)
> > {
> > last if $ftp1->put("$FileBodyName.tar.gz");
> > LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> > }

>
> > In other words, do the loop 5 times unless one of the put() calls
> > succeeds. If that happens, exit early with `last'.

>
> > Ted

>
> Thanks Ted - looks like a good solution so I will try.
> Snorik, this one suits best I think as your solution ignored exit status (if
> thats the right word in perl) of previous attempt(s) . i.e. if first attempt
> succeeds then no need to retry


Yes, this is completely right, I forgot about that actually Teds
solution is shorter, without being illegible.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-08-2009
banzai wrote:

> Actually, one more question
>
> Could a smiliar 5x loop be put around the whole ftp sequence i.e. login, cwd
> and put so tha the whole process is repeated 5 times if required (not just
> the put) and exit early if the put succeeds
>
>


Sure. Why do you think it wouldn't work? Actually, maybe it'd
be better to figure out why it's failing to work the first time.

You'll have to exit out of the loop correctly, probably easiest to
use a label.

perldoc -f last
 
Reply With Quote
 
Snorik
Guest
Posts: n/a
 
      01-09-2009
On 8 Jan., 15:22, Tad J McClellan <(E-Mail Removed)> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
>
>
>
> Snorik <(E-Mail Removed)> wrote:
> > On 8 Jan., 11:25, "banzai" <(E-Mail Removed)> wrote:
> >> Need help with the following automated ftp script please;

>
> >> if (defined ($ftp1)) {
> >> $ftp1->login($FTP1user,$FTP1password) or LogError("FTP login to
> >> $FTP1server failed");
> >> if (defined $FTP1destination){
> >> $ftp1->cwd($FTP1destination) or LogError("Could not change directory
> >> to $FTP1destination on $FTP1server");
> >> }
> >> $ftp1->binary;
> >> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> >> $FileBodyName.tar.gz to $FTP1server");
> >> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> >> $DataTagName.gz to $FTP1server");
> >> $ftp1->quit;} else {

>
> >> LogError("FTP open failed to $FTP1server, error code: $!");}

>
> >> #

>
> >> I need to modify the above so that the ftp transfers (put's) are attempted
> >> up to 5 times in case the remote server does not respond. No time delay

>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> >> needed between retries.

>
> >> TIA

>
> > Why not simply use a counter variable in a while loop?

>
> Because if the remote server _does_ respond, it will upload the
> same file 5 times when once was enough.
>
> The solution must somewhere include a test of whether the server
> responded or not.


You are right, as said elsethread, I did not think of that at all.

> > my $count = 0;
> > while ($count <= 4)

>
> That is an error-prone way to do it.
>
> foreach my $count ( 0 .. 4 )
> or
> foreach my $count ( 1 .. 5 )
>
> is much easier to read (and therefore, easier to get right).


Yes, indeed, thanks for pointing that out, I had completely forgotten
about this possibility.
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      01-13-2009
On Thu, 8 Jan 2009 15:18:30 -0000 "banzai" <(E-Mail Removed)> wrote:

b> "Ted Zlatanov" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <(E-Mail Removed)> wrote:
>>

b> $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
b> $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy $DataTagName.gz to $FTP1server");
>>

b> I need to modify the above so that the ftp transfers (put's) are attempted
b> up to 5 times in case the remote server does not respond. No time delay
b> needed between retries.
>>
>> Each one can be done like so (untested code):
>>
>> for (1..5)
>> {
>> last if $ftp1->put("$FileBodyName.tar.gz");
>> LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
>> }
>>
>> In other words, do the loop 5 times unless one of the put() calls
>> succeeds. If that happens, exit early with `last'.


b> Could a smiliar 5x loop be put around the whole ftp sequence i.e. login, cwd
b> and put so tha the whole process is repeated 5 times if required (not just
b> the put) and exit early if the put succeeds

Yes, but then you won't know what failed, and you don't want to do the
first put 5 times if the second one failed 5 times.

You probably want to avoid repeating the loop all over your code, which
is good. I hate repetition too. Perl lets you create anonymous
subroutines, which are basically a block of executable code you can pass
as a variable. With those, you can do:

# untested: try N times to run CODE, and do ERR every time it fails
sub tryN
{
my $N = shift @_;
my $code = shift @_;
my $err = shift @_;

for (1..$N)
{
last if $code->();
$err->();
}
}

# now your program is a bunch of these statements
tryN(5,
sub { $ftp1->put("$FileBodyName.tar.gz") },
sub { LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server"); });

There are some scoping rules at play, but nothing you need to worry
about with a simple program. You should probably comment this carefully
if it's going to be used and maintained.

On Thu, 8 Jan 2009 08:15:27 -0800 (PST) Snorik <(E-Mail Removed)> wrote:

S> Teds solution is shorter, without being illegible.

I really tried to balance between legible and concise, so I'm glad you
liked it.

Thanks
Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Script to be automated Graham Feeley Perl Misc 4 07-06-2008 04:29 AM
Automated testing of cgi / perl spam@comjet.com Perl Misc 4 02-18-2007 04:00 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Automated Perl to Python translation? Stephen Ferg Python 6 09-07-2004 11:54 PM
Net::FTP problems getting files from Windows FTP server, but not Linux FTP Server. D. Buck Perl Misc 2 06-29-2004 02:05 PM



Advertisments