Skip to content

Instantly share code, notes, and snippets.

@tobiisdesired
Created June 2, 2023 11:14
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 tobiisdesired/3dd34c3a6a3b29933ca8f8306723b0d1 to your computer and use it in GitHub Desktop.
Save tobiisdesired/3dd34c3a6a3b29933ca8f8306723b0d1 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
$connections = [
{"user@host1.com" => "development server"},
{"user@host2.com" => "QA test"}
]
Signal.trap("SIGINT") do
puts ("\nexit")
exit
end
def max_conn_size
$connections.inject(0) do |max, element|
element.keys.first.size > max.to_i ? element.keys.first.size : max
end
end
def print_connections
$connections.each.with_index do |connection, index|
conn, description = connection.first
printf "%#{$connections.size.to_s.length}d) %-#{max_conn_size+3}s %s\n", index+1, conn, description
end
end
def request_selection
print "Pick a server (or ctrl-c to quit): "
system("stty raw -echo")
input = Integer(STDIN.getc)
ensure
system("stty -raw echo")
print "#{input}\n"
end
def connect(selection)
connection = $connections[selection-1].keys.first
puts "Connecting to #{connection}"
exec("ssh -X #{connection}")
end
def get_selection
if(ARGV[0])
selection = Integer(ARGV[0])
else
print_connections
selection = request_selection
end
if selection.between?(1, $connections.size)
selection
else
raise ArgumentError
end
rescue ArgumentError
puts "Invalid Argument. Selection must be between 1 and #{$connections.size}"
exit -1
end
if __FILE__ == $0
connect get_selection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment