in reply to interact with remote host via bastion
You want Net::SSH or Net::SSH::Perl which is pure Perl and does not shell out to ssh(1)
use Net::SSH qw(sshopen2); use strict; my $user = "username"; my $host = "hostname"; my @cmds = ( "command1", "command2", "etc" ); my %results; for my $cmd ( @cmds ) { sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $! +"; my $result = ''; $result .= $_ while <READER>; $results{$cmd} = $result; } close READER; close WRITER;
This will build a hash $results with the key:value pairs of the command and its result. Note you will lose results if you exec exactly the same command twice as hash keys are unique. If you just want an array of the results do:
my @results; for my $cmd ( @cmds ) { sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $! +"; my $result = ''; $result .= $_ while <READER>; push @results, $result; }
Note if you don't want to use DSA or RSA and wish to supply a login password use Net::SSH::Perl
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: interact with remote host via bastion
by jaco (Pilgrim) on Feb 15, 2003 at 21:44 UTC | |
by tachyon (Chancellor) on Feb 15, 2003 at 23:42 UTC |