Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Port Forwarding with Net::SSH::Expect

by naChoZ (Curate)
on Dec 21, 2007 at 21:22 UTC ( #658541=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Port Forwarding with Net::SSH::Expect
Select or Download Code
Re^2: Port Forwarding with Net::SSH::Expect
by jrsimmon (Hermit) on Dec 21, 2007 at 22:40 UTC
    Ok, so at first this post confused me, seems at first glance to be functionally equivalent...but then i realized the last thing I did before going to be last night was to "clean up" my code and put each session into its own subroutine.

    I pulled subs and created on long script like you have here, and it works like a charm. I don't quite understand why that would make a difference...maybe something with the scope of the various $ssh vars?

    Thanks much for the help! ++

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://658541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-11-23 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (127 votes), past polls