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;

In reply to Check availability of multiple FTP servers by delirium

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.