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

ive a @server_list and i connect to each $server in it using net::ssh::perl like so:
foreach $server (@server_list) { $ssh = Net::SSH::Perl->new($server); ... }
however it connects to them one after another, and i want/need to connect to them simoultaneously. anyone have any suggestions?

Replies are listed 'Best First'.
Re: simoultaneous connections using Net::SSH::Perl
by Aristotle (Chancellor) on Sep 15, 2002 at 16:41 UTC
    It should work to do it this way:
    my @connection; foreach (@server_list) { my $ssh = Net::SSH::Perl->new($_) or next; push @connection, $ssh; } foreach my $ssh (@connection) { # ... }
    Alternatively, if this isn't simultaneous enough, you might want to look at fork or modules like Parallel::ForkManager.

    Makeshifts last the longest.

Re: simoultaneous connections using Net::SSH::Perl
by jj808 (Hermit) on Sep 15, 2002 at 18:52 UTC
    Here's some example code with the fork()'s in place:
    #! /usr/bin/perl -w use strict; $SIG{CHLD} = \&REAPER; my @server_list = qw/one two three four/; my $children = 0; foreach my $server (@server_list) { print "$$ Forking for server: $server\n"; if (my $pid = fork) { # Parent process # Nothing to do except go to the next server in the list $children++; sleep 2; next; } else { # Child process # Note that you *must* use 'exit;' when processing is # finished or the child will continue going through the # loop print "$$ Processing for server: $server\n"; sleep 3; exit; } } # Wait for children to finish while ($children > 0) { sleep; } sub REAPER { my $pid = wait; print "Process $pid has done it's stuff!\n"; $children--; }
    When a forked process dies, the parent process will get a SIGCHLD signal (I am assuming you are using UNIX). Unless the parent does something with this signal, the child process will becoma a 'zombie', and eventually the process table will fill up (not good). If you don't need to keep track of the children as they finish, do $SIG{CHLD} = 'IGNORE'. I've put some delays in the example code so you can see this happening.

    You should also be aware that as with any system call, a fork() can fail. Read page 715 of 'Programming Perl, 3rd edition' for more info.

    JJ

Re: simoultaneous connections using Net::SSH::Perl
by mem (Acolyte) on Sep 15, 2002 at 17:02 UTC
    foreach $server (@server_list) { $ssh = Net::SSH::Perl->new($server); ... }

    What are the ...? You are not spawning processes with each new call, you are just opening network connections. If you really want to spawn a process, you are missing a fork in there.

Re: simoultaneous connections using Net::SSH::Perl
by jj808 (Hermit) on Sep 15, 2002 at 19:08 UTC
    *** I know I've posted this twice, because the question was duplicated and I didn't know which one would get reaped. If this is against etiquette, please /msg me.

    Here's some example code with the fork()'s in place:

    #! /usr/bin/perl -w use strict; $SIG{CHLD} = \&REAPER; my @server_list = qw/one two three four/; my $children = 0; foreach my $server (@server_list) { print "$$ Forking for server: $server\n"; if (my $pid = fork) { # Parent process # Nothing to do except go to the next server in the list $children++; sleep 2; next; } else { # Child process # Note that you *must* use 'exit;' when processing is # finished or the child will continue going through the # loop print "$$ Processing for server: $server\n"; sleep 3; exit; } } # Wait for children to finish while ($children > 0) { sleep; } sub REAPER { my $pid = wait; print "Process $pid has done it's stuff!\n"; $children--; }
    When a forked process dies, the parent process will get a SIGCHLD signal (I am assuming you are using UNIX). Unless the parent does something with this signal, the child process will becoma a 'zobmie', and eventually the process table will fill up (not good). If you don't need to keep track of the children as they finish, do $SIG{CHLD} = 'IGNORE'. I've put some delays in the example code so you can see this happening.

    You should also be aware that as with any system call, a fork() can fail. Read page 715 of 'Programming Perl, 3rd edition' for more info.

    JJ