Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Looping Through FTP Addresses

by he204035 (Novice)
on Feb 27, 2012 at 07:50 UTC ( [id://956392]=perlquestion: print w/replies, xml ) Need Help??

he204035 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote some code that will open a text file, which contains a list of servers and their ftp addresses. My code goes through the list of servers, ftp's to each, and pulls specific files, etc.

The code works fine until the Net::FTP comes upon an address that is not accessible for some reason. I would like the script to carry on through the loop of each address, but once he hits that first connect timeout, he can't connect to any other addresses, even if I know they are good.

Here is an example of a manual connect to a server that I can connect to manually (addresses and login info changed):

[root@abcdefgjmp09 PegCount]# ftp 10.222.233.244 Connected to 10.252.156.103. 220 wxyz-bts01 FTP server ready. 530 Please login with USER and PASS. 530 Please login with USER and PASS. KERBEROS_V4 rejected as an authentication type Name (10.222.233.244:root): xxx 331 Password required for xxx. Password: 230 User xxx logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp>

But, when run via script, this address is the 5th in the list. The 1st two are good (can connect), and then he runs into to 2 that are unreachable at the moment. So when we get to the fifth one, I would expect the script to still be able to connect, but he can't:

Connected to CA5 Logged in to CA5 Directory changed on CA5. Got file list from CA5 Getting CA5.201202270000.txt... # Disconnected from CA5 Connected to CA2 Logged in to CA2 Directory changed on CA2. Got file list from CA2 Getting CA2.201202270000.txt... # Disconnected from CA2 Can't ftp to 10.222.200.200: Net::FTP: connect: timeout FTP to 10.222.200.200 aborted. Script paused for a few seconds... Can't ftp to 10.222.201.201: Net::FTP: connect: timeout FTP to 10.222.201.201 aborted. Script paused for a few seconds... Can't ftp to 10.222.233.244: <<address reachable manually FTP to 10.252.156.103 aborted.

Here is the first part of my FTP section of my script...

@records = <BTS>; foreach(@records) { my @files=(); my @sorted_seen=(); my $host=(); ($clli,$host) = split /;/, $_, 2; chomp $host; $ftp=Net::FTP->new($host,Timeout=>15) or $newerr=1; if($newerr) { print "\n\nCan't ftp to $host: $@\n"; print " FTP to $host aborted.\n\n"; next; } else { print "\n\nConnected to $clli\n"; <<continue with file transfer, etc>>

If I move this address to say, 3rd in the list, instead of after the two failures, the script connects to him just fine.

Sorry for the short novel...I have even tried using $ftp->quit but it comes back stating basically that you can't quit an ftp session you were never connected to.

Replies are listed 'Best First'.
Re: Looping Through FTP Addresses
by repellent (Priest) on Feb 27, 2012 at 08:21 UTC
    Initialize $newerr = 0 for each iteration of your foreach loop.

      Yup, always the simple stuff that bites me. Thanks so much! Been beating my head against a wall. Really hurts and starting to affect my vision, I think (joke there).

      Thanks again!

Re: Looping Through FTP Addresses
by JavaFan (Canon) on Feb 27, 2012 at 09:13 UTC
    Just eliminate the $newerr variable.
    my $ftp = Net::Ftp->new($host, Timeout => 15) or do { print "Cannot ftp to $host: $@\n"; next; }; print "Connect to $cli\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://956392]
Approved by repellent
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found