Although I often use ssh tunnels in curious ways such as this, I've never actually played with
Net::SSH::Expect until now. I got this working just fine. I just rigged it up so that I can ssh from my workstation to box3 via localhost port 4101, I didn't bother trying other ports.
I switched the $ssh1 object method to run_ssh() because the lab machines I used to test this already have ssh keys set up already.
One thing that was very important, the sleep statements. It would not work until I put both of them in there. I'm assuming it must be some sort of race condition, but I'm not sure what the proper way to handle this.
And the $ssh_params I added are just arguments that I normally use to stuff ssh into the background when making tunnels.
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Expect;
use Data::Dumper;
my $user = "user";
my $pwd = undef;
my $localhost = 'localhost';
my $first_host = 'box1';
my $second_host = 'box2';
my $third_host = 'box3';
my $local_port_one = 4100;
my $local_port_two = 4101;
my $ssh_port = 22;
my $raw_pty = 1;
my $timeout = 3;
my $ssh_params = '-P -N -f '; # send ssh to the background
my $rc;
my $rc2;
my $ssh1 = Net::SSH::Expect->new(
host => $first_host,
password => $pwd,
user => $user,
raw_pty => $raw_pty,
timeout => $timeout,
ssh_option => "${ssh_params} -L ${local_port_one}:${second_host}"
. ":${ssh_port}"
);
$Data::Dumper::Varname = 'rc_died_';
$rc = $ssh1->run_ssh() or die Dumper( @! );
sleep 1;
my $ssh2 = Net::SSH::Expect->new(
host => $localhost,
#password => "$pwd",
user => $user,
raw_pty => $raw_pty,
timeout => $timeout,
ssh_option => "${ssh_params} -p ${local_port_one} -L "
. "${local_port_two}:${third_host}:${ssh_port} ",
);
$Data::Dumper::Varname = 'rc2_died_';
$rc2 = $ssh2->run_ssh() or die Dumper( @! );
sleep 1;
--
naChoZ
Therapy is expensive. Popping bubble wrap is cheap. You choose.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.