Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Last active December 16, 2015 07:29
Show Gist options
  • Save EronHennessey/5398988 to your computer and use it in GitHub Desktop.
Save EronHennessey/5398988 to your computer and use it in GitHub Desktop.
sshme - opens a shell to known location using an alias.
#!/usr/bin/ruby
#
# sshme - opens a shell to a known location, defined in the user's ~/.sshme file
#
# by Eron Hennessey
#
# The .sshme file is simply a file that contains a list of aliases to ssh
# locations, like this:
#
# serveralias1 path.to.server1
# serveralias2 [email protected]
#
# No spaces are allowed in either the aliases or in the ssh locations. If you
# want to set ssh arguments, do it in the SSH constant.
#
# The ssh command (or whatever can take similar arguments). You can modify this
# if you want ssh to include special arguments before the location.
SSH = "ssh"
# Some insults, in case the user (that would be me) is clueless and needs help.
INSULTS = [
"cad", "blackguard", "scoundrel", "toad", "good-for-nothing", "worm", "rat",
"lummox", "dolt", "oaf", "bumpkin", "idiot", "fool", "donkey", "wretch",
"goats-for-brains"
]
# A function to print the known locations.
def print_locations
puts "** I know about these locations:"
# LOCATIONS is defined and populated later in the script.
LOCATIONS.each { |k,v|
puts "#{k}\t => #{v}"
}
end
#
# Here's the script...
#
# The path to the user configuration file (~/.sshme on unixes).
CONFIGFILE = File.join(Dir.home, ".sshme")
if(!File.exists? CONFIGFILE) then
puts "** Whoa #{INSULTS.shuffle.first}, there's no config file at #{CONFIGFILE}!"
puts "You need to fill it with values like this:"
puts "serveralias1 path.to.server1"
puts "serveralias2 [email protected]"
exit 1
end
# A hash of locations loaded from the user's config file.
LOCATIONS = Hash[*File.read(File.join(Dir.home, ".sshme")).split(/\s+/)]
# accept only one argument: the location.
if(ARGV.length != 1) then
puts "Usage: sshme <location>"
exit 0
end
# special command to list locations
if(ARGV[0] == "list") then
print_locations
exit 0
end
# build the command and execute it!
loc = LOCATIONS[ARGV[0]]
# check to see if the user typed in an unknown location.
if loc.nil? then
# if he did, reprimand him first, then show him the known locations.
puts "** Hey #{INSULTS.shuffle.first}, I don't know anything about \"#{ARGV[0]}\"! :P"
print_locations
exit 0
end
cmd = (SSH + " " + LOCATIONS[ARGV[0]])
exec cmd
@EronHennessey
Copy link
Author

Since I can't remember all of the full URLs of servers I ssh into (and some of them have quite bizarre names), I've created this simple Ruby script to ssh to an easy-to-remember alias, instead.

These aliases and corresponding locations are stored in the user's home directory in a file called .sshme, which contains a number of lines with aliases and corresponding server locations, formatted like this:

server    [email protected]
laptop    eron@laptophostname:my/favorite/dir
desktop   eron@desktophostname

Any amount of whitespace is allowed between the alias and server location, but whitespace cannot be present within the alias or server location.

I put the script itself in ~/bin, name it "sshme" and make it executable. Windows users may need to create a .bat that calls this with the ruby executable.

If you grab this via git clone, you can also use ln -s /path/to/sshme.rb ~/bin/sshme to make it an executable command in your bash environment.

Example syntax:

$ sshme laptop

To get a listing of locations that you entered in the ~/.sshme file:

$ sshme locations

NOTE: This script requires ruby version 1.9 or newer, as it uses the Dir.home variable, which doesn't resolve on version 1.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment