Net-SSH-Tunnel
view release on metacpan or search on metacpan
lib/Net/SSH/Tunnel.pm view on Meta::CPAN
ssh -f -N -L 10000:dest.example.com:22 <effective username>@hostname.example.com
after the driver script is done, you can then do:
ssh -p 10000 user@localhost
other usages:
Usage: ./driver.pl --port 10000 --host dest.example.com --hostport 22 --hostname hostname.example.com
Sets up a ssh tunnel. Works on both local and remote forwarding.
In the example above, it will create a tunnel from your host to
hostname.example.com, where your local port 10000 is forwarded to
dest.example.com's port 22.
--hostname specify the host where you create a tunnel from your host
--host specify the destination of port forwarding
--user user when connecting to <hostname>. default: effective user
--type specify local or remote, for forwarding. default: local
--hostport target port on <host>. default: 22
--port source port for forwarding. default: 10000
--sshport equivalent of -p <port> in ssh client. default: 22
--action 'setup' or 'destroy' a tunnel. default: setup
--help prints the usage and exits
--debug turn on debug messages
Notes on testing:
This module wraps around ssh and as such, requires authentication.
I have included test_deeply.pl that asks for hostnames, runs ssh and establishes a tunnel.
If you'd like to test manually, please use the script.
=head1 SUBROUTINES/METHODS
=head2 new
The constructor. Creates an object, invokes init() for argument parsing
=cut
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->init();
return $self;
}
=head2 init
Arg parser. Sets default values, uses Getopt::Long then do the necessary parsing.
=cut
sub init {
my $self = shift;
my $opts = {
hostport => 22,
port => 10000,
type => 'local',
action => 'setup',
help => 0,
debug => 0,
user => scalar( getpwuid($>) ),
sshport => 22,
};
GetOptions(
$opts,
'hostname=s',
'host=s',
'type=s',
'hostport=i',
'port=i',
'user=s',
'sshport=i',
'destroy' => sub { $opts->{ action } = 'destroy' },
'help' => \$opts->{ help },
'debug' => sub { $opts->{ debug }++ }, # for various debug levels, if needed
);
$self->usage() if ( !$opts->{ hostname } || !$opts->{ host } || $opts->{ type } !~ /local|remote/ || $opts->{ help } );
Log::Log4perl->easy_init($DEBUG) if $opts->{ debug };
$self->{ opts } = $opts;
chomp( $self->{ cmds }->{ ssh } = `which ssh` );
chomp( $self->{ cmds }->{ ps } = `which ps` );
chomp( $self->{ cmds }->{ grep } = `which grep` );
croak "ssh, ps or grep not found" unless( -x $self->{ cmds }->{ ssh } && -x $self->{ cmds }->{ ps } && -x $self->{ cmds }->{ grep } );
}
=head2 run
Driver method to do the new()->init() dance, then calls appropriate methods based on the args
=cut
sub run {
my $class = shift;
my $self = $class->new();
my $action = $self->{ opts }->{ action };
if ( $action eq 'setup' ) {
DEBUG "Setting up tunnel";
$self->setup_tunnel();
}
elsif( $action eq 'destroy' ) {
DEBUG "Destroying tunnel";
$self->destroy_tunnel();
}
return $self;
}
=head2 setup_tunnel
Establishes a ssh tunnel based on the object info.
=cut
sub setup_tunnel {
my $self = shift;
( run in 1.869 second using v1.01-cache-2.11-cpan-39bf76dae61 )