Skip to content

Instantly share code, notes, and snippets.

@epylinkn
Created July 18, 2013 03:07
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 epylinkn/6026417 to your computer and use it in GitHub Desktop.
Save epylinkn/6026417 to your computer and use it in GitHub Desktop.
ssh-config => a ruby script for easily managing your ssh config shortcuts so that you don't need to type beasts like: ssh -i ~/.ssh/thisisapemkey.pem ubuntu@ec2-amazon-has-really-difficult-hostnames.amazonaws.com
#!/usr/bin/env ruby
require 'thor'
class SSHConfig < Thor
desc "list", "list all hosts"
def list()
f = File.open("/Users/#{ENV['USER']}/.ssh/config", 'r')
hosts = f.select { |line| line =~ /Host\s/ }
hosts.map { |host| host[4..-1].strip }.each do |host|
puts host
end
end
desc "add [HOST] [HOSTNAME]", "add HOST shortcut for HOSTNAME to ssh config"
method_options :user => :string, :identity_file => :string
def add(host, host_name)
f = File.open("/Users/#{ENV['USER']}/.ssh/config", 'a')
f << "Host #{host}\n"
f << "HostName #{host_name}\n"
f << "User #{options[:user]}\n" if options[:user]
f << "IdentityFile #{options[:identity_file]}\n" if options[:identity_file]
f << "\n"
f.close
end
end
SSHConfig.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment