Given the following script:
#!/usr/bin/perl while (<STDIN>) { chomp; open(H, "ssh $_ 'uptime' 2>&1 |"); $b = <H>; close(H); printf("%s: %s\n", $_, $b); }
and a file hosts.dat containing the the host names
larry curley moe
and assuming appropriate ssh keys in place and passphases entered, I'd expect the output of
./test.pl <hosts
to be something along the line of
larry: 22:20:46 up 53 days, 1:27, 1 user, load average: 0.06, 0.04 +, 0.05 curley: 22:20:46 up 67 days, 7:56, 1 user, load average: 0.07, 0.0 +5, 0.05 moe: 22:20:47 up 12 days, 11:22, 1 user, load average: 0.04, 0.04, + 0.05
Instead, I get only the first line before the program exits. Commenting out these two lines:
$b = <H>; close(H);
allows the script to hit all three hosts, but without the requested uptime info. Uncommenting either line reverts to single line output. Also prefixing the ssh command with 'echo' correctly echos the command for all three hosts.

Based on my tinkering it would appear that reading any input from the ssh command or closing it's filehandle is somehow clobbering perl's STDIN stream. I can think of a number of possible ways around this situation, but I'm curious about why it's happening in the first place.


In reply to Odd perl/ssh interaction by mlf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.