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
    I looked into this, and while it would work fairly well via one host, the bastion in the middle causes some problems.

    perhaps i should have explained that more clearly earlier.

    localhost ->ssh to bastion ->ssh to remotehost->command

    i'd like to keep the chain alive.

    Thanks for your help, but it looks like i'm going to have to rewrite the pty code in order to pull this off.

      Just write a passthrough script for the bastion. All you have to do is SSH into the bastion and fire up the passthrough script. Pass commands to this script which then passes them on to another host and printing the resulting output to STDOUT which by default will go back to you localhost. Such a script might look like this:

      #!/usr/bin/perl # passthrough SSH script use Net::SSH qw(sshopen2); use strict; my $user = "username"; my $host = "hostname"; while ( my $cmd = <> ) { last if $cmd =~ /exit_passthrough/; sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || print "ssh: +$!"; my $result = ''; $result .= $_ while <READER>; print $result; } close READER; close WRITER;

      Effectively this creates a basic SSH secured shell in the middle for you. A bonus is that you can get the script to work on the bastion box and then step back to localhost to make debugging straight forward.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print