direct_ssh 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +1 -0
- data/bin/direct_ssh +35 -0
- data/direct_ssh.gemspec +28 -0
- data/lib/direct_ssh.rb +23 -0
- data/lib/direct_ssh/key_handler.rb +92 -0
- data/lib/direct_ssh/validator.rb +31 -0
- data/lib/direct_ssh/version.rb +3 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Xia Xiongjun
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# DirectSsh
|
2
|
+
|
3
|
+
In order to use ssh without the need to enter password everytime,
|
4
|
+
this gem will create public/private rsa keys if they do not exist
|
5
|
+
and send public key to remote server
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'direct_ssh'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install direct_ssh
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/direct_ssh
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'direct_ssh'
|
6
|
+
require 'net/ssh'
|
7
|
+
|
8
|
+
def usage
|
9
|
+
puts 'usage: direct_ssh user@host [-p port]'
|
10
|
+
puts ' The default port 22 will be used if you leave it off'
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_args(argv)
|
14
|
+
raise ArgumentError, 'Invalid argument' unless argv.join(' ') =~ /^([^ ]+)@([^ ]+)( -p ([^ ]+))?$/
|
15
|
+
user = $1
|
16
|
+
host = $2
|
17
|
+
port = ($4 == nil) ? '22' : $4
|
18
|
+
[host, user, port]
|
19
|
+
end
|
20
|
+
|
21
|
+
def main(argv)
|
22
|
+
host, user, port = parse_args(argv)
|
23
|
+
|
24
|
+
DirectSsh.start(host, user, {:port => port}) { |ssh|
|
25
|
+
puts ssh.exec!('cat /etc/*-release')
|
26
|
+
puts 'Direct SSH connected successfully'
|
27
|
+
}
|
28
|
+
rescue ArgumentError => e
|
29
|
+
$stderr.puts e.message
|
30
|
+
usage
|
31
|
+
rescue Net::SSH::AuthenticationFailed => e
|
32
|
+
$stderr.puts e.message
|
33
|
+
end
|
34
|
+
|
35
|
+
main(ARGV)
|
data/direct_ssh.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'direct_ssh/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "direct_ssh"
|
10
|
+
spec.version = DirectSsh::VERSION
|
11
|
+
spec.authors = ["Xia Xiongjun"]
|
12
|
+
spec.email = ["xxjapp@gmail.com"]
|
13
|
+
spec.description = %q{Create public/private rsa keys if they do not exist and send public key to remote server}
|
14
|
+
spec.summary = %q{Use ssh without the need to enter password everytime}
|
15
|
+
spec.homepage = %q{http://rubygems.org/gems/direct_ssh}
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "net-ssh"
|
24
|
+
spec.add_runtime_dependency "highline"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
data/lib/direct_ssh.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'direct_ssh/validator'
|
6
|
+
require 'direct_ssh/key_handler'
|
7
|
+
|
8
|
+
module DirectSsh
|
9
|
+
def self.start(host, user, options={}, &block)
|
10
|
+
validator = Validator.new
|
11
|
+
ssh = validator.start(host, user, options)
|
12
|
+
|
13
|
+
KeyHandler.send_key_to_remote(ssh) if !validator.direct
|
14
|
+
|
15
|
+
if block_given?
|
16
|
+
retval = yield ssh
|
17
|
+
ssh.close
|
18
|
+
return retval
|
19
|
+
else
|
20
|
+
return ssh
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
#
|
4
|
+
|
5
|
+
module KeyHandler
|
6
|
+
def self.send_key_to_remote(ssh)
|
7
|
+
ssh_public_key = get_ssh_public_key
|
8
|
+
send_ssh_public_key_to_remote(ssh, ssh_public_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
################################################################
|
12
|
+
# local ssh key process
|
13
|
+
|
14
|
+
# get public key, create it if not exists
|
15
|
+
def self.get_ssh_public_key
|
16
|
+
if !File.exists?(Dir.home + '/.ssh/id_rsa.pub')
|
17
|
+
create_ssh_files
|
18
|
+
chmod_ssh_files
|
19
|
+
|
20
|
+
private_key = OpenSSL::PKey::RSA.new(2048)
|
21
|
+
public_key = get_public_key(private_key.public_key)
|
22
|
+
|
23
|
+
File.write(Dir.home + '/.ssh/id_rsa', private_key)
|
24
|
+
File.write(Dir.home + '/.ssh/id_rsa.pub', public_key)
|
25
|
+
|
26
|
+
return public_key
|
27
|
+
end
|
28
|
+
|
29
|
+
IO.read(Dir.home + '/.ssh/id_rsa.pub')
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.create_ssh_files
|
33
|
+
FileUtils.mkdir_p Dir.home + '/.ssh'
|
34
|
+
FileUtils.touch Dir.home + '/.ssh/id_rsa'
|
35
|
+
FileUtils.touch Dir.home + '/.ssh/id_rsa.pub'
|
36
|
+
FileUtils.touch Dir.home + '/.ssh/authorized_keys'
|
37
|
+
FileUtils.touch Dir.home + '/.ssh/known_hosts'
|
38
|
+
end
|
39
|
+
|
40
|
+
# see: http://www.noah.org/wiki/SSH_public_keys
|
41
|
+
def self.chmod_ssh_files
|
42
|
+
FileUtils.chmod 0700, Dir.home + '/.ssh'
|
43
|
+
FileUtils.chmod 0600, Dir.home + '/.ssh/id_rsa'
|
44
|
+
FileUtils.chmod 0644, Dir.home + '/.ssh/id_rsa.pub'
|
45
|
+
FileUtils.chmod 0644, Dir.home + '/.ssh/authorized_keys'
|
46
|
+
FileUtils.chmod 0644, Dir.home + '/.ssh/known_hosts'
|
47
|
+
end
|
48
|
+
|
49
|
+
# see: http://www.rubydoc.info/github/delano/rye/Rye/Key.public_key_to_ssh2
|
50
|
+
def self.get_public_key(public_key)
|
51
|
+
authtype = public_key.class.to_s.split('::').last.downcase
|
52
|
+
b64pub = Base64.encode64(public_key.to_blob).strip.gsub(/[\r\n\t ]/, '')
|
53
|
+
user = ENV['USER']
|
54
|
+
host = ENV['HOSTNAME']
|
55
|
+
host = ENV['COMPUTERNAME'] if host == nil
|
56
|
+
"ssh-%s %s %s@%s" % [authtype, b64pub, user, host]
|
57
|
+
end
|
58
|
+
|
59
|
+
################################################################
|
60
|
+
# remote ssh key process
|
61
|
+
|
62
|
+
def self.send_ssh_public_key_to_remote(ssh, key)
|
63
|
+
if !remote_file_exists?(ssh, '~/.ssh/authorized_keys')
|
64
|
+
remote_create_ssh_files(ssh)
|
65
|
+
remote_chmod_ssh_files(ssh)
|
66
|
+
end
|
67
|
+
|
68
|
+
# append public_key to remote '~/.ssh/authorized_keys'
|
69
|
+
ssh.exec!("echo '#{key}' >> ~/.ssh/authorized_keys")
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.remote_file_exists?(ssh, path)
|
73
|
+
ssh.exec!("[ ! -f #{path} ] && echo NOT_EXIST") == nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.remote_create_ssh_files(ssh)
|
77
|
+
ssh.exec!('mkdir -p ~/.ssh')
|
78
|
+
ssh.exec!('touch ~/.ssh/id_rsa')
|
79
|
+
ssh.exec!('touch ~/.ssh/id_rsa.pub')
|
80
|
+
ssh.exec!('touch ~/.ssh/authorized_keys')
|
81
|
+
ssh.exec!('touch ~/.ssh/known_hosts')
|
82
|
+
end
|
83
|
+
|
84
|
+
# see: http://www.noah.org/wiki/SSH_public_keys
|
85
|
+
def self.remote_chmod_ssh_files(ssh)
|
86
|
+
ssh.exec!('chmod 700 ~/.ssh')
|
87
|
+
ssh.exec!('chmod 600 ~/.ssh/id_rsa')
|
88
|
+
ssh.exec!('chmod 644 ~/.ssh/id_rsa.pub')
|
89
|
+
ssh.exec!('chmod 644 ~/.ssh/authorized_keys')
|
90
|
+
ssh.exec!('chmod 644 ~/.ssh/known_hosts')
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'net/ssh'
|
6
|
+
require 'highline/import'
|
7
|
+
|
8
|
+
class Validator
|
9
|
+
attr_reader :direct
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@direct = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def start(host, user, options={})
|
16
|
+
return Net::SSH.start(host, user, options)
|
17
|
+
rescue Net::SSH::AuthenticationFailed
|
18
|
+
@direct = false
|
19
|
+
|
20
|
+
3.times {
|
21
|
+
options[:password] = ask("#{user}@#{host}'s password: ") { |q| q.echo = false }
|
22
|
+
|
23
|
+
begin
|
24
|
+
return Net::SSH.start(host, user, options)
|
25
|
+
rescue Net::SSH::AuthenticationFailed
|
26
|
+
end
|
27
|
+
}
|
28
|
+
|
29
|
+
raise Net::SSH::AuthenticationFailed, 'Permission denied, please try again.'
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: direct_ssh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Xia Xiongjun
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-03 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: '0'
|
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: '0'
|
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: '0'
|
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: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Create public/private rsa keys if they do not exist and send public key
|
79
|
+
to remote server
|
80
|
+
email:
|
81
|
+
- xxjapp@gmail.com
|
82
|
+
executables:
|
83
|
+
- direct_ssh
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- bin/direct_ssh
|
93
|
+
- direct_ssh.gemspec
|
94
|
+
- lib/direct_ssh.rb
|
95
|
+
- lib/direct_ssh/key_handler.rb
|
96
|
+
- lib/direct_ssh/validator.rb
|
97
|
+
- lib/direct_ssh/version.rb
|
98
|
+
homepage: http://rubygems.org/gems/direct_ssh
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.8.23
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Use ssh without the need to enter password everytime
|
123
|
+
test_files: []
|