in reply to Odd perl/ssh interaction

I think that the script could do with some improvements - Use strict and warnings, three arg open with proper error checking, error checking on the close due to the piped open, and using <> instead of <STDIN> would be some best practices that would also make the script more robust and easily debuggable in case of errors.

Anyway, I think huck is on to something. I can reproduce the problem when I run the program as ./1194547.pl <hosts.txt, but when I run the program as cat hosts.txt | perl 1194547.pl, it works. Also, adding the </dev/null to the command as huck suggested makes it work as well, as does using the ssh -n switch, which has the same effect. So while I haven't dug into the details, changing the uptime to cat does seem to show that the problem is ssh is reading STDIN and sending it to the remote command.

However, as I wrote about at length here, shelling out to an external process is not always a good idea, especially when you can't trust the user input. The great Net::OpenSSH module, while it does also use external ssh processes, does this in a more robust fashion, plus it provides a whole lot more functionality (Net::OpenSSH::Parallel might be of interest to you). I would strongly recommend using that module instead of shelling out to ssh yourself.

Replies are listed 'Best First'.
Re^2: Odd perl/ssh interaction
by mlf (Initiate) on Jul 08, 2017 at 13:12 UTC
    Agreed regarding strict/warn, error checking, <> and such. The original script had most of that, but I stripped it out as I trimmed down to a minimal test case. Also agree in principle the the Net::OpenSSH suggestion, though I work in an environment where 'extra' modules are not always easily/readily available. They were on my list of possible work-arounds.

    Good call on the 'cat' substitution. That definitely highlights the problem. Ultimately '-n' will be the best solution in this case.

    Thanks for the help!

      I work in an environment where 'extra' modules are not always easily/readily available.

      Note that Net::OpenSSH is a pure-Perl module, and so in the worst case can be installed by copying it over. Only if you need password authentication the XS based module IO::Pty needs to be installed.

      Update:

      Agreed regarding strict/warn, error checking, ... I stripped it out as I trimmed down to a minimal test case.

      The effort to create an SSCCE is appreciated, but it's best to leave those things in when posting questions here, otherwise most monks' first thought will be "how do we know the problem isn't being hidden because those things are missing"? :-)