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.


In reply to Re: Port Forwarding with Net::SSH::Expect by naChoZ
in thread Port Forwarding with Net::SSH::Expect by jrsimmon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.