mlf has asked for the wisdom of the Perl Monks concerning the following question:
and a file hosts.dat containing the the host names#!/usr/bin/perl while (<STDIN>) { chomp; open(H, "ssh $_ 'uptime' 2>&1 |"); $b = <H>; close(H); printf("%s: %s\n", $_, $b); }
and assuming appropriate ssh keys in place and passphases entered, I'd expect the output oflarry curley moe
to be something along the line of./test.pl <hosts
Instead, I get only the first line before the program exits. Commenting out these two lines: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
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.$b = <H>; close(H);
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Odd perl/ssh interaction
by huck (Prior) on Jul 08, 2017 at 03:49 UTC | |
by mlf (Initiate) on Jul 08, 2017 at 13:33 UTC | |
|
Re: Odd perl/ssh interaction
by haukex (Archbishop) on Jul 08, 2017 at 10:49 UTC | |
by mlf (Initiate) on Jul 08, 2017 at 13:12 UTC | |
by haukex (Archbishop) on Jul 08, 2017 at 13:41 UTC | |
|
Re: Odd perl/ssh interaction
by Laurent_R (Canon) on Jul 08, 2017 at 08:54 UTC | |
by afoken (Chancellor) on Jul 08, 2017 at 15:19 UTC | |
by mlf (Initiate) on Jul 08, 2017 at 13:29 UTC |