adamcooke-key-installer 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +25 -0
  2. data/bin/install-key +3 -0
  3. data/lib/key_installer.rb +72 -0
  4. metadata +55 -0
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = SSH Key Installation
2
+
3
+ A tiny script to copy your SSH public keys from your local machine to
4
+ a remote server. It'll search your .ssh/ folder for known public keys
5
+ and if it finds one, it'll add it to the remote authorized_keys file.
6
+
7
+ == Install
8
+
9
+ gem sources -a http://gems.github.com
10
+ sudo gem install adamcooke-key-installer
11
+
12
+ == Usage
13
+
14
+ Install the default public key to a server called myserver.com for a
15
+ user called 'dave'
16
+
17
+ install-key dave@myserver.com
18
+
19
+ Install a key from /home/user/my_key.pub to a server
20
+
21
+ install-key phil@anotherserver.com --key /home/user/my_key.pub
22
+
23
+ Install default keys to a server running an un-standard SSH port
24
+
25
+ install-key sarah@mylittlebox.com -p 12345
data/bin/install-key ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + "/../lib/key_installer"
3
+ KeyInstaller.run(ARGV)
@@ -0,0 +1,72 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ module KeyInstaller
4
+
5
+ ## A very simple script to push an SSH key to a remote host via. SSH.
6
+
7
+ extend self
8
+
9
+ attr_reader :options
10
+
11
+ def run(args)
12
+ install_key(args.shift, args)
13
+ end
14
+
15
+ private
16
+
17
+ def install_key(host, args)
18
+
19
+ if host.nil?
20
+ puts " ** Hostname was not provided (e.g. install-key dave@myserver.com)"
21
+ return
22
+ end
23
+
24
+ args_to_options(args)
25
+ if options.keys.include?('key')
26
+ if File.exist?(options['key'])
27
+ public_key = File.read(options['key'])
28
+ else
29
+ puts " ** Key not found in '#{options['key']}'"
30
+ end
31
+
32
+ else
33
+ ## Find a key from the .ssh folder
34
+ for possible_key in %w{id_rsa.pub id_dsa.pub}
35
+ path_to_key = File.join(ENV['HOME'], '.ssh', possible_key)
36
+ if File.exist?(path_to_key)
37
+ public_key = File.read(path_to_key)
38
+ break
39
+ end
40
+ end
41
+ end
42
+
43
+ ## Set the command to run on the remote server:
44
+ ssh_command = %Q{echo '#{public_key}' | ssh #{host} -p #{ssh_port} "mkdir -p .ssh && cat - >> .ssh/authorized_keys"}
45
+
46
+ if %x[ #{ssh_command} ] == ''
47
+ puts " ** Key installed to '#{host}' successfully."
48
+ else
49
+ puts " ** Sorry, something went wrong and the world will now end."
50
+ end
51
+
52
+ end
53
+
54
+ def ssh_port
55
+ @options['port'] || @options['p'] || 22
56
+ end
57
+
58
+ def args_to_options(args)
59
+ @options = {} unless @options
60
+ c = 0
61
+ for arg in args
62
+ value = args[c + 1] || ""
63
+ value = nil if value.include?("-")
64
+ @options[arg.gsub(/-/, "")] = value if arg.include?("-")
65
+ c += 1
66
+ end
67
+ size = @options.size * 2
68
+ args.slice!(0 - size, size)
69
+ end
70
+
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adamcooke-key-installer
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.1"
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: adam@atechmedia.com
18
+ executables:
19
+ - install-key
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - bin/install-key
26
+ - lib/key_installer.rb
27
+ - README.rdoc
28
+ has_rdoc: false
29
+ homepage: http://www.atechmedia.com
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Tiny utility to install SSH keys onto remote machines
54
+ test_files: []
55
+