Last active
August 29, 2015 14:03
-
-
Save tekwiz/36ffebec77567cb6d65a to your computer and use it in GitHub Desktop.
ssh-tunnel command
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
#!/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