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. | [reply] [d/l] |
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 | [reply] [d/l] [select] |
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. | [reply] [d/l] |
#! /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 | [reply] [d/l] [select] |