ssh-copy-id.rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ssh-copy-id.rb.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Junegunn Choi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # ssh-copy-id.rb
2
+
3
+ A Ruby implementation of `ssh-copy-id` script. (Surprise!)
4
+ Supports copying to/removing from multiple servers.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ gem install ssh-copy-id.rb
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```
15
+ usage: ssh-copy-id.rb [-i IDENDITY] [-u USER] [-p PASSWORD] [USER[:PASSWORD]@]HOSTNAME[:PORT] ...
16
+
17
+ -i, --identity=IDENTITY Identity file. Default: ~/.ssh/id_rsa.pub
18
+ -u, --username=USERNAME Username. Default: current user.
19
+ -p, --password[=PASSWORD] Password. Ask password if not given.
20
+ ```
21
+
22
+ ## Examples
23
+
24
+ ```ruby
25
+ ssh-copy-id.rb server1 server2:9022 user@server{2..5} user:password@server{6..10}
26
+ ssh-remove-id.rb server1 server2:9022 user@server{2..5} user:password@server{6..10}
27
+ ```
28
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'ssh-copy-id'
5
+ require 'optparse'
6
+
7
+ options = SSHCopyID.parse_options(__FILE__, ARGV)
8
+ SSHCopyID.grant({:hosts => ARGV.uniq, :output => STDOUT}.merge(options))
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'ssh-copy-id'
5
+ require 'optparse'
6
+
7
+ options = SSHCopyID.parse_options(__FILE__, ARGV)
8
+ SSHCopyID.revoke({:hosts => ARGV.uniq, :output => STDOUT}.merge(options))
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+ require 'ssh-copy-id/version'
3
+ require 'net/ssh'
4
+ require 'highline'
5
+
6
+ module SSHCopyID
7
+ DEFAULT_IDENTITY_FILE = '~/.ssh/id_rsa.pub'
8
+
9
+ def self.grant options
10
+ execute options, true do |identity|
11
+ %[
12
+ /bin/bash -cl '
13
+ umask 077;
14
+ test -d ~/.ssh || mkdir ~/.ssh;
15
+ if [ ! -f ~/.ssh/authorized_keys -o `grep "#{identity}" ~/.ssh/authorized_keys 2> /dev/null | wc -l` -eq 0 ]; then
16
+ echo "#{identity}" >> ~/.ssh/authorized_keys
17
+ echo "Key added."
18
+ else
19
+ echo "Key already exists."
20
+ fi
21
+ '
22
+ ]
23
+ end
24
+ end
25
+
26
+ def self.revoke options
27
+ execute options, false do |identity|
28
+ %[
29
+ /bin/bash -cl '
30
+ umask 077;
31
+ if [ -f ~/.ssh/authorized_keys -a `grep "#{identity}" ~/.ssh/authorized_keys 2> /dev/null | wc -l` -ge 1 ]; then
32
+ grep -v "#{identity}" ~/.ssh/authorized_keys | cat > ~/.ssh/authorized_keys.ruby-ssh-copy-id
33
+ mv -f ~/.ssh/authorized_keys.ruby-ssh-copy-id ~/.ssh/authorized_keys
34
+ echo "Key removed."
35
+ else
36
+ echo "Key not found."
37
+ fi
38
+ '
39
+ ]
40
+ end
41
+ end
42
+
43
+ def self.execute options, ask_password
44
+ identity_file = options[:identity] || DEFAULT_IDENTITY_FILE
45
+ identity = File.readlines(File.expand_path identity_file).first.chomp
46
+ password = options[:password]
47
+ output = options[:output] || Class.new { def method_missing *args; end }.new
48
+
49
+ options[:hosts].each do |host|
50
+ host, user = host.split('@').reverse
51
+ user, pass = user.split(':') if user
52
+ user ||= options[:username] || ENV['USER']
53
+ output.print "#{user}@#{host}: "
54
+ if pass.nil? && password.nil? && (ask_password || options.has_key?(:password))
55
+ password = HighLine.new.ask($/ + "Password: ") { |q| q.echo = '*' }
56
+ end
57
+ pass ||= password
58
+
59
+ host, port = host.split(':')
60
+ Net::SSH.start(host, user, :port => port, :password => pass) do |ssh|
61
+ result = ssh.exec!(yield identity)
62
+ output.puts result
63
+ end
64
+ end
65
+ rescue Net::SSH::AuthenticationFailed
66
+ output.puts "Authentication failed."
67
+ exit 1
68
+ end
69
+
70
+ def self.parse_options filename, argv
71
+ {}.tap { |options|
72
+ opts = OptionParser.new { |opts|
73
+ opts.banner =
74
+ "usage: #{File.basename filename} [-i IDENDITY] [-u USER] [-p PASSWORD] [USER[:PASSWORD]@]HOSTNAME[:PORT] ..."
75
+ opts.separator ''
76
+
77
+ opts.on('-i', '--identity=IDENTITY', 'Identity file. Default: ~/.ssh/id_rsa.pub') do |v|
78
+ options[:identity] = v
79
+ end
80
+
81
+ opts.on('-u', '--username=USERNAME', 'Username') do |v|
82
+ options[:username] = v
83
+ end
84
+
85
+ opts.on('-p', '--password[=PASSWORD]', 'Password. Ask password if not given.') do |v|
86
+ options[:password] = v
87
+ end
88
+
89
+ opts.separator ''
90
+ opts.on_tail('-h', '--help', "Show this message") do
91
+ puts opts
92
+ exit
93
+ end
94
+ }
95
+ begin
96
+ opts.parse!(argv)
97
+ rescue SystemExit => e
98
+ exit e.status
99
+ rescue Exception => e
100
+ puts e.to_s
101
+ puts opts
102
+ exit 1
103
+ end
104
+
105
+ if argv.empty?
106
+ puts opts
107
+ exit 1
108
+ end
109
+ }
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module SSHCopyID
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ssh-copy-id/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ssh-copy-id.rb"
8
+ gem.version = SSHCopyID::VERSION
9
+ gem.authors = ["Junegunn Choi"]
10
+ gem.email = ["junegunn.c@gmail.com"]
11
+ gem.description = %q{ssh-copy-id in Ruby. Supports copying to multiple servers.}
12
+ gem.summary = %q{ssh-copy-id in Ruby. Supports copying to multiple servers.}
13
+ gem.homepage = "https://github.com/junegunn/ssh-copy-id.rb"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_runtime_dependency 'net-ssh', '>= 2.6'
20
+ gem.add_runtime_dependency 'highline', '>= 1.6.15'
21
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
5
+ load File.join(File.dirname(__FILE__), '../bin/', File.basename(ARGV.shift))
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssh-copy-id.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: highline
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.15
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.15
46
+ description: ssh-copy-id in Ruby. Supports copying to multiple servers.
47
+ email:
48
+ - junegunn.c@gmail.com
49
+ executables:
50
+ - ssh-copy-id.rb
51
+ - ssh-remove-id.rb
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/ssh-copy-id.rb
61
+ - bin/ssh-remove-id.rb
62
+ - lib/ssh-copy-id.rb
63
+ - lib/ssh-copy-id/version.rb
64
+ - ssh-copy-id.rb.gemspec
65
+ - test/bin_helper.rb
66
+ homepage: https://github.com/junegunn/ssh-copy-id.rb
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: ssh-copy-id in Ruby. Supports copying to multiple servers.
90
+ test_files:
91
+ - test/bin_helper.rb
92
+ has_rdoc: