Skip to content

Instantly share code, notes, and snippets.

@adamw523
Created May 9, 2011 18:22
Show Gist options
  • Save adamw523/963052 to your computer and use it in GitHub Desktop.
Save adamw523/963052 to your computer and use it in GitHub Desktop.
Expose your private port on a public server over SSH
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