Skip to content

Instantly share code, notes, and snippets.

@billpatrianakos
Last active August 24, 2019 15:33
Show Gist options
  • Save billpatrianakos/d635ccde39cdcb6bb18b9861cd7694f0 to your computer and use it in GitHub Desktop.
Save billpatrianakos/d635ccde39cdcb6bb18b9861cd7694f0 to your computer and use it in GitHub Desktop.
Connect over SSH without having to remember domains or IP addresses
#!/usr/bin/env ruby
# Connect
#
# Quickly SSH into a server or get its details without
# having to remember IP addresses or domain names.
#
# SETUP
# -----
# Fill out the following:
# `DEFAULT_USERNAME` - The username so ssh with if none is specified in the command
# `hosts` - An array of host hashes where each has a name, IP, and description. Follow the example in the code.
# Put this script somewhere in your path like /usr/local/bin and make sure it's executable (`mv connect.rb /usr/local/bin/connect && chmod +x /usr/local/bin/connect`
#
# Usage: Run `connect <hostname> <user>`
# <user> is optional and defaults to '<REPLACE WITH YOUR OWN DEFAULT>'
# List: `connect list`
# Lists the hostname, IP, and description for each host.
##
# EDIT YOUR USERNAME
##
DEFAULT_USER = 'CHANGE_ME'
if ARGV[0].nil?
puts <<-END
Please specify a hostname. USAGE:
connect <host> <username> - Username is optional
END
exit 1
end
command = ARGV[0] == 'list' ? 'list' : 'connect'
host = ARGV[0]
user = ARGV[1].nil? ? DEFAULT_USER : ARGV[1] # REPLACE 'user' with your preferred default username
##
# ADD YOUR HOSTS HERE
##
hosts = [
{
name: 'portland',
ip: '10.0.0.123',
description: 'Portland potentiometer driven synth',
user: user
},
{
name: 'olympic',
ip: '10.0.0.167',
description: 'A Raspberry Pi 4 B with undetermined use',
user: user
}
]
##
# NOTHING ELSE TO EDIT
##
if command == 'connect'
command_str = ''
hosts.each do |h|
if h[:name] == host
command_str = "ssh #{h[:user]}@#{h[:ip]}"
end
end
puts "Connecting #{user} to #{host}..."
system(command_str)
else
hosts.each do |host|
hostname = host[:name].rjust(15)
ip = host[:ip].rjust(15)
desc = host[:description]
puts "#{hostname} | #{ip} | #{desc}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment