This code uses Parallel::ForkManager to spawn multiple FTP sessions to a list of servers, verifying connectivity and logon credentials.

It takes a comma-delimited list from STDIN in the format:
server handle,address(:port),username,password$/

#!/usr/bin/perl -w use Net::FTP; use Parallel::ForkManager; use strict; my %srvs = (); my $num_forks = 10; my $timeout = 30; my $pm = new Parallel::ForkManager($num_forks); $pm->run_on_start( sub { print STDERR "Connecting to $_[1], port $srvs +{$_[1]}{port}\n"; } ); $pm->run_on_finish ( sub { my (undef, $exit_code, $ident) = @_; if ( $exit_code == 0 ) { $srvs{$ident}{stat} = "Good logon to $ +ident\n"; } elsif ( $exit_code == 1 ) { $srvs{$ident}{stat} = "*** Logon to $i +dent failed\n"; } elsif ( $exit_code == 2 ) { $srvs{$ident}{stat} = "*** Connect to +$ident failed\n"; } else { $srvs{$ident}{stat} = " Script error while connecting to $i +dent\n"; } print STDERR $srvs{$ident}{stat}; } ); sub ftpcheck { my $id = shift; my $srv=$srvs{$id}; my $status = 1; my $ftp=Net::FTP->new($$srv{addr}, Timeout=>$timeout, Port=>$$srv{ +port}); exit(2) if ! $ftp; $status = 0 if $ftp->login($$srv{user},$$srv{pass}); $ftp->quit(); # Be nice to the server and send QUIT whether or no +t login worked exit ($status); } while (<>) { chomp; next unless $_; my @F = split /,/,$_,4; next unless $#F == 3; if ( $F[1] =~ /([^:]+):([^:]+)/ ) { $srvs{$F[0]}{addr} = $1; $srvs{$F[0]}{port} = $2; } else { $srvs{$F[0]}{addr} = $F[1]; $srvs{$F[0]}{port} = 21; } $srvs{$F[0]}{user} = $F[2]; $srvs{$F[0]}{pass} = $F[3]; $srvs{$F[0]}{stat} = '*** Unknown'; } for my $key ( keys %srvs ) { my $pid = $pm->start($key) and next; &ftpcheck($key); $pm->finish($key); } $pm->wait_all_children; print $srvs{$_}{stat} for sort keys %srvs;

Replies are listed 'Best First'.
Re: Check availability of multiple FTP servers
by princepawn (Parson) on Oct 09, 2003 at 18:29 UTC
    cute. may I slap this into the errata directory of Net::FTP::Common? :)

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

      I couldn't tell if you were being sarcastic or not, so I consulted my magic 8-ball. It said "Try again later."

      But, uh, sure. Knock yourself out.

        No, I am not being sarcastic. Thanks. I will credit you in the code.

        Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

Re: Check availability of multiple FTP servers
by ptkdb (Monk) on Oct 17, 2003 at 13:08 UTC
    How about have it parse the .netrc file, the file that allows you to autologin to FTP sites to validate it? That would be a handy facility to have.

    ## ## typical .netrc entry ## machine hostname login username password password