Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 77 lines (57 sloc) 1.786 kB
d8c85d8 @jwang more empty templates and some config added
authored
1 #!/bin/env ruby -w
2
3 # Originally from http://github.com/adamcooke/key-installer/tree/master
4
5 module KeyInstaller
6
7 ## A very simple script to push an SSH key to a remote host via. SSH.
8
9 extend self
10
11 attr_reader :options
12
13 def run(args)
14 install_key(args.shift, args)
15 end
16
17 private
18
19 def install_key(host, args)
20
21 if host.nil?
22 puts " ** Hostname was not provided (e.g. install-key dave@myserver.com)"
23 return
24 end
25
26 args_to_options(args)
27 if options.keys.include?('key')
28 if File.exist?(options['key'])
29 public_key = File.read(options['key'])
30 else
31 puts " ** Key not found in '#{options['key']}'"
32 end
33
34 else
35 ## Find a key from the .ssh folder
36 for possible_key in %w{id_rsa.pub id_dsa.pub}
37 path_to_key = File.join(ENV['HOME'], '.ssh', possible_key)
38 if File.exist?(path_to_key)
39 public_key = File.read(path_to_key)
40 break
41 end
42 end
43 end
44
45 ## Set the command to run on the remote server:
46 ssh_command = %Q{echo '#{public_key}' | ssh #{host} -p #{ssh_port} "mkdir -p .ssh && cat - >> .ssh/authorized_keys2"}
47
48 if %x[ #{ssh_command} ] == ''
49 puts " ** Key installed to '#{host}' successfully."
50 else
51 puts " ** Sorry, something went wrong and the world will now end."
52 end
53
54 end
55
56 def ssh_port
57 @options['port'] || @options['p'] || 22
58 end
59
60 def args_to_options(args)
61 @options = {} unless @options
62 c = 0
63 for arg in args
64 value = args[c + 1] || ""
65 value = nil if value.include?("-")
66 @options[arg.gsub(/-/, "")] = value if arg.include?("-")
67 c += 1
68 end
69 size = @options.size * 2
70 args.slice!(0 - size, size)
71 end
72
73
74 end
75
76 KeyInstaller.run(ARGV)
Something went wrong with that request. Please try again.