Skip to content

Instantly share code, notes, and snippets.

@krimdomu
Last active December 11, 2015 04:48
Show Gist options
  • Save krimdomu/4547448 to your computer and use it in GitHub Desktop.
Save krimdomu/4547448 to your computer and use it in GitHub Desktop.
Using a jumphost
use Rex -base;
user "foouser";
task "uptime", "your-server", sub {
say run "uptime";
};
before uptime => sub {
my ($server, $server_ref) = @_;
# create local redirect to your-server on port 9999
system("ssh -fN -L9999:$server:22 your-user\@your-jumphost");
$$server_ref = "localhost:9999"; # rewrite server reference to localhost:9999
};
after uptime => sub {
my ($server) = @_;
my ($localhost, $port) = split(/:/, $server);
# get the redirecting process
my ($process) = grep { $_->{command} =~ m/^ssh \-fN \-L$port:$localhost/ } ps;
kill $process->{pid};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment