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