http://qs321.pair.com?node_id=658541


in reply to Port Forwarding with Net::SSH::Expect

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.