Click here to get back home

Re: ssh ssh

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: ssh ssh jammer 03-27-2008
Posted by jammer on March 27, 2008, 3:35 pm
Please log in for more thread options
>
>
>
>
> > > I am trying to write a script that takes a list of hosts and sshs into
> > > the first one and then can ssh to other ones. I can only ssh to the
> > > other hosts from the first host.
>
> > > Here is what I tried:
> > > I think it is waiting for the ssh to the first host to finish.
>
> > > I guess I could scp a partial hostlist and a program to *.domain and
> > > then run the program remotely.
> > > Am I on a right track?
>
> > 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),
>
> > > # print `ssh $line`;
>
> > 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.

That is a good basis.
I am using that plus copying a script over.


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap