| jammer 2008-03-27, 10:16 pm |
| On Mar 27, 1:02 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth jammer <jamesloc...@mail.com>:
>
>
>
>
> No. Step back a minute and consider how you would do this without Perl:
> what you want to end up running is
>
> ssh host1.domain ssh host2 uname -a
>
> assuming ssh is in your default PATH on host1.domain. What you actually
> end up running, here (or would if it weren't commented),
>
>
> is
>
> ssh host1.domain
>
> with no command specified. This will give you a shell; while it would be
> possible to remote-control that shell, it's much easier to use ssh's
> ability to run a command directly. You want something like
>
> open my $HOSTLIST, '<', 'hostlist3.txt'
> or die "can't open hostlist3.txt: $!";
>
> $/ = ''; # this will read a paragraph at a time
> $\ = "\n"; # avoids needing to print it all the time
>
> while (<$HOSTLIST> ) {
> my $cmd = join ' ', map "ssh $_", split /\n/;
> $cmd .= ' uname -a';
> print "executing '$cmd'";
> print `$cmd`;
> }
>
> which will cope with any number of intervening hosts automatically. Note
> that this assumes none of the items in hostlist3.txt have spaces in:
> annoyingly, there isn't a form of backticks corresponding to system
> LIST, so that would be rather harder to deal with.
>
> Ben
Here is my next attempt:
#!/bin/perl
use strict;
my $inputFile = 'hostlist3.txt';
open my $HOSTLIST, '<', $inputFile
or die "can't open hostlist: $!";
$/ = ''; # this will read a paragraph at a time
$\ = "\n"; # avoids needing to print it all the time
while (<$HOSTLIST> ) {
my @hostList = split /\n/;
my $adminHost = shift( @hostList ); # first line
print 'adminHost=' . $adminHost;
my $otherHosts = join ' ', map "ssh $_ uname -a", @hostList;
my $cmd = "ssh ccn\@$adminHost $otherHosts";
print "executing '$cmd'";
print `$cmd`;
}
The problem is that I can't seem to run more than one command from
ssh.
I don't really want to open and ssh connection for each otherHosts.
What if hostlist3.txt is:
host1.domain
host2
host3
host4.domain
....
I need 'ssh host1.domain ssh host2 uname -a ssh host3 uname -a'.
I can do 'ssh host1.domain ssh host2 uname -a' and 'ssh host1.domain
ssh host3 uname -a' but I'd rather not ssh to host1.domain more than
once.
|