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

Hello,

I have created a snmp agent in perl.

I use this piece of code to launch sub process :

foreach my $disk (@tab_disks) { my $pid = fork(); if ($pid) { push(@childs, $pid); } elsif ($pid == 0) { $logger->info("Creating new sub-process"); check_disk($logger, $disk); $logger->info("Exit sub-process"); exit; } }
In check_disk function I have this piece of code :
my $cmd = 'ssh -qt ' . $disk->{controller_ipaddr} . ' "sudo '. $CMD_ST +ORCLI . ' /c0/e' . $disk->{physical_id} . '/s' . $disk->{slot_id} . ' + show 2>&1"'; $logger->debug("Execute command : " . $cmd); open COMD, "$cmd|" or die "Cannot execute $cmd: $!"; while (my $line = <COMD>) { $logger->debug("Line : " . $line); chomp($line); if ($line !~ /$disk->{physical_id}/) { next; } $line =~ s/^\s+|\s+$//g; if (exists($DISK_STATUS{$line})) { $new_status_txt = $line; $new_status = $DISK_STATUS{$line}; $logger->debug("New status is : " . $new_status); } else { $new_status_txt = $line; $new_status = $DISK_STATUS{Other}; $logger->debug("New status is : " . $new_status); } } close COMD; $logger->debug("Command exit status : " . $?);
I have about 20 entries in tab @tab_disks. On this 20 entries, 2-3 random ssh command launch with open returns exit code 65280. It is not all the same.

Thanks for your help.

Regargds,

Samuel Mutel.

Replies are listed 'Best First'.
Re: Strange behaviour with SSH command launched with open
by salva (Canon) on Jun 04, 2014 at 11:42 UTC
    Try using Net::OpenSSH::Parallel.

    It would take care of the low level details and can retry failed commands automatically.

Re: Strange behaviour with SSH command launched with open
by RichardK (Parson) on Jun 04, 2014 at 09:59 UTC

    If you have a look at the help for system it says :-

             If you'd like to manually inspect "system"'s failure, you can
                check all possible failure modes by inspecting $? like this:
    
                    if ($? == -1) {
                        print "failed to execute: $!\n";
                    }
                    elsif ($? & 127) {
                        printf "child died with signal %d, %s coredump\n",
                            ($? & 127),  ($? & 128) ? 'with' : 'without';
                    }
                    else {
                        printf "child exited with value %d\n", $? >> 8;
                    }
    

    Which will give you 'child exited with value 255', so ssh failed returning -1, therefore check your ssh setup.

      Thanks for your help. Another question: how can I display error message returning by the ssh command with open command. I tried 2>&1 but it seems to not work correctly. Regards, Samuel Mutel.

        I'm not sure, but IPC::Open3 might give you better results.

      It's not exactly a question about perl :

      - Do you know if SSHD limit the number of concurrent process ?

      - Same question with open ?

      Regards,

      Samuel Mutel.