Created
May 9, 2011 18:22
-
-
Save adamw523/963052 to your computer and use it in GitHub Desktop.
Expose your private port on a public server over SSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tunnel < Thor | |
method_options :environment => "development", :aliases => "-e", :desc => "Config environment" | |
desc "start", "Start an ssh tunnel" | |
def start | |
random_hi_port = rand(30000) + 30000 | |
identity_part = tunnel['identity_file'] ? "-i #{tunnel['identity_file']}" : "" | |
ssh_part = "ssh -t #{identity_part} -g -R *:#{random_hi_port}:0.0.0.0:#{tunnel['local_port']} #{tunnel['username']}@#{tunnel['public_host']}" | |
socat_cmd = "socat TCP-LISTEN:#{tunnel['public_port']},fork,reuseaddr TCP:localhost:#{random_hi_port}" | |
command = "#{ssh_part} #{socat_cmd}" | |
puts "executing #{command}" | |
exec command | |
end | |
private | |
def tunnel | |
@tunnel ||= YAML.load_file(File.expand_path('tunnel.yml'))[options[:environment]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment