use warnings; use strict; use Net::SSH::Perl; use Parallel::ForkManager; ### Add your own. my $p_host = ''; my $d_host = ''; my $ssh_key_file = ''; ### For command execution. my @cmds = ( 'ls', 'ls -l', 'ls -1', ); my ($out, $exit); ### Establish two connections. Works fine. my $p_ssh = Net::SSH::Perl->new( $p_host, identity_files => [$ssh_key_file], ); $p_ssh->login && print "$p_host connected.\n"; my $d_ssh = Net::SSH::Perl->new( $d_host, identity_files => [$ssh_key_file], ); $d_ssh->login && print "$d_host connected.\n"; ### Try the commands on each host. Works fine. for my $cmd (@cmds) { print "Trying '$cmd'...\n"; print '=' x 20, "\n"; for my $server ($d_ssh, $p_ssh) { ($out, undef, $exit) = $server->cmd($cmd); print "$out\n\n"; die if $exit; } } ### Now attempt the same thing in a fork. Bad news. for my $cmd (@cmds) { print "Trying '$cmd'...\n"; print '=' x 20, "\n"; my $pfm = new Parallel::ForkManager(2); for my $host ($d_ssh, $p_ssh) { my $pid = $pfm->start and next; ($out, undef, $exit) = $host->cmd($cmd); print "$out\n\n"; die if $exit; $pfm->finish; } $pfm->wait_all_children; } #### host1 connected. host2 connected. Trying 'ls'... ==================== ...listing from host1... ...listing from host2... Trying 'ls -l'... ==================== ...listing from host1... ...listing from host2... Trying 'ls -1'... ==================== ...listing from host1... ...listing from host2... Trying 'ls'... ==================== ...listing from host1... ...listing from host2... Trying 'ls -l'... ==================== Bad packet length 3032450590 at /usr/local/lib/perl5/site_perl/5.10.0/Net/SSH/Perl/Packet.pm line 175 Bad packet length 629981479 at /usr/local/lib/perl5/site_perl/5.10.0/Net/SSH/Perl/Packet.pm line 175 Trying 'ls -1'... ==================== Use of uninitialized value $out in concatenation (.) or string at ./tmp.pl line 53. Connection failed: Connection reset by peer at ./tmp.pl line 52