Skip to content

Instantly share code, notes, and snippets.

@tekwiz
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tekwiz/36ffebec77567cb6d65a to your computer and use it in GitHub Desktop.
Save tekwiz/36ffebec77567cb6d65a to your computer and use it in GitHub Desktop.
ssh-tunnel command
#!/usr/bin/env ruby
require "optparse"
require "ostruct"
$options = OpenStruct.new
$options.verbose = false
$options.local_port = nil
$opts = OptionParser.new do |o|
o.banner = "Usage: ssh-tunnel [options] host port"
o.separator ""
o.separator "Open an SSH tunnel to the given host and port."
o.separator ""
o.on("-p", "--port [PORT]", Integer, "Bind to local port.") do |port|
$options.local_port = port
end
#TODO reverse tunnel option (see ssh -R [bind_address:]port:host:hostport)
#TODO --no-forward tunnel option
#TODO
# o.on("-v", "--[no-]verbose", "Run verbosely") do |v|
# $options.verbose = v
# end
o.on_tail("-h", "--help", "Show this message") do
puts o
exit
end
end
$opts.parse!(ARGV)
if ARGV.length != 2
puts $opts
exit 1
end
$options.host = ARGV.shift
$options.port = ARGV.shift
$options.local_port ||= $options.port
cmd = sprintf "ssh -Nn -L %s:localhost:%s %s",
$options.local_port, $options.port, $options.host
printf "Executing `%s`...\n", cmd
exec cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment