Skip to content

Instantly share code, notes, and snippets.

@jaylevitt
Created December 30, 2011 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaylevitt/1541080 to your computer and use it in GitHub Desktop.
Save jaylevitt/1541080 to your computer and use it in GitHub Desktop.
Gateway ssh
#!/usr/bin/env ruby
# gwssh: ssh via a gateway host. Just use gwssh instead of ssh,
# and your options, commands, etc. will be preserved.
GATEWAY_HOST = 'gate.example.com'
DOMAIN = 'example.com'
DEBUG = false
OPTIONS_WITH_ARGUMENTS = 'bcDeFiLlmOopRSw'
def main
host = get_hostname
ssh = %w{ssh -t -A}
original_host = host.dup
unless host =~ /\d+\.\d+\.\d+\.\d+/ || host.end_with?(DOMAIN)
host += ".#{DOMAIN}"
end
args = ARGV.dup
args[args.index(original_host)] = host
if host == GATEWAY_HOST
command = [*ssh, *args]
else
command = [*ssh, GATEWAY_HOST, *ssh, *args]
end
puts command.inspect if DEBUG
Kernel.exec(*command)
end
# The hostname will be the first argument that isn't an option (-v)
# and that isn't an argument for an option (-F file).
def get_hostname
trash = []
ARGV.each_with_index do |arg, index|
next if trash.include?(index)
if arg.start_with?('-')
trash << index
if OPTIONS_WITH_ARGUMENTS.include?(arg[1])
trash << index + 1
end
end
end
other_args = ARGV.reject.with_index { |_, i| trash.include?(i) }
other_args.first
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment