direct_ssh 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -47,7 +47,6 @@ b. scripts which expose password directly
47
47
  # encoding: UTF-8
48
48
 
49
49
  require 'net/ssh'
50
- require 'highline/import'
51
50
 
52
51
  Net::SSH.start('127.0.0.1', 'user', {:password => 'password'}) { |ssh|
53
52
  puts ssh.exec!('cat /etc/*-release')
@@ -86,7 +85,7 @@ ssh.close
86
85
 
87
86
  ## Shell
88
87
 
89
- The direct_ssh shell command checks status of ssh connection to the remote server. It will ask password and send public key to remote server if neccessary.
88
+ The `direct_ssh` shell command checks status of ssh connection to the remote server. It will ask password and send public key to remote server if neccessary.
90
89
 
91
90
  After ssh connection created successfully, The `cat /etc/*-release` command will be executed.
92
91
 
@@ -95,6 +94,20 @@ Usage: direct_ssh user@host [-p port]
95
94
  - default port 22 will be used if you leave it off
96
95
  ```
97
96
 
97
+ The `direct_scp` shell command uses direct_ssh to upload or download files.
98
+
99
+ ```text
100
+ Usage:
101
+ 1) upload file to remote server
102
+ # direct_scp [-r] [-p port] local_file user@host:remote_file
103
+ 2) download file from remote server
104
+ # direct_scp [-r] [-p port] user@host:remote_file local_file
105
+ 3) print this message
106
+ # direct_scp -h
107
+
108
+ default port 22 will be used if you leave it off
109
+ ```
110
+
98
111
  ## Contributing
99
112
 
100
113
  1. Fork it
@@ -102,3 +115,9 @@ Usage: direct_ssh user@host [-p port]
102
115
  3. Commit your changes (`git commit -am 'Add some feature'`)
103
116
  4. Push to the branch (`git push origin my-new-feature`)
104
117
  5. Create new Pull Request
118
+
119
+ ## Links
120
+
121
+ 1. ruby gem [net-ssh](https://rubygems.org/gems/net-ssh)
122
+ 2. ruby gem [highline](https://rubygems.org/gems/highline)
123
+ 3. ruby gem [direct_ssh](https://rubygems.org/gems/direct_ssh)
data/bin/direct_scp ADDED
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require 'direct_ssh'
5
+ require 'net/ssh'
6
+ require 'net/scp'
7
+
8
+ def usage
9
+ puts 'Usage:'
10
+ puts '1) upload file to remote server'
11
+ puts ' # direct_scp [-r] [-p port] local_file user@host:remote_file'
12
+ puts '2) download file from remote server'
13
+ puts ' # direct_scp [-r] [-p port] user@host:remote_file local_file'
14
+ puts '3) print this message'
15
+ puts ' # direct_scp -h'
16
+ puts
17
+ puts 'default port 22 will be used if you leave it off'
18
+ end
19
+
20
+ def parse_args(argv)
21
+ if argv[0] == '-h'
22
+ usage; exit
23
+ end
24
+
25
+ @recursive = false
26
+ @port = 22
27
+
28
+ 0.upto(argv.length - 3) { |i|
29
+ @recursive = true if argv[i] == '-r'
30
+ @port = argv[i + 1] if argv[i] == '-p'
31
+ }
32
+
33
+ argv[-2] =~ /((.*)@(.*):)?(.*)/
34
+ @user = $2 if $2 != nil
35
+ @host = $3 if $3 != nil
36
+ @download = ($2 != nil && $3 != nil)
37
+ @src_file = $4
38
+
39
+ argv[-1] =~ /((.*)@(.*):)?(.*)/
40
+ @user = $2 if $2 != nil
41
+ @host = $3 if $3 != nil
42
+ @upload = ($2 != nil && $3 != nil)
43
+ @dst_file = $4
44
+
45
+ raise ArgumentError, 'Invalid argument' if !@user || !@host || !@src_file || !@dst_file || !@download && !@upload
46
+ end
47
+
48
+ def terminal_width
49
+ HighLine::SystemExtensions.terminal_size[0]
50
+ end
51
+
52
+ def limit_string(str, max_length)
53
+ if max_length >= str.length
54
+ return str
55
+ elsif max_length > 2
56
+ return '..' + str[str.length - max_length + 2, max_length - 2]
57
+ else
58
+ return '..'
59
+ end
60
+ end
61
+
62
+ MIN_MAX_DOT_LENGTH = 5
63
+
64
+ def scp_progress_info(file, sent, total)
65
+ if total != 0
66
+ percent = sent.to_f / total.to_f
67
+ else
68
+ percent = 1
69
+ end
70
+
71
+ if @upload
72
+ string1 = " upload - #{file} "
73
+ else
74
+ string1 = " download - #{file} "
75
+ end
76
+
77
+ string3 = sprintf("%4d%%", percent * 100)
78
+
79
+ max_dot_length = terminal_width - 2 - (string1 + string3).length
80
+
81
+ if max_dot_length < MIN_MAX_DOT_LENGTH
82
+ limit_file = limit_string(file, file.length - MIN_MAX_DOT_LENGTH + max_dot_length)
83
+ return scp_progress_info(limit_file, sent, total)
84
+ end
85
+
86
+ dot_string = '-' * (percent * max_dot_length).ceil + '>' + ' ' * ((1 - percent) * max_dot_length).floor
87
+ return string1 + dot_string + string3
88
+ end
89
+
90
+ def show_transfer_progress(old_file, file, sent, total)
91
+ puts if file != old_file && old_file != nil
92
+ print "\r" + scp_progress_info(file, sent, total)
93
+ return file
94
+ end
95
+
96
+ def main(argv)
97
+ parse_args(argv)
98
+
99
+ DirectSsh.start(@host, @user, {:port => @port}) { |ssh|
100
+ old_file = nil
101
+
102
+ if @upload
103
+ ssh.scp.upload!(@src_file, @dst_file, {:recursive => @recursive}) { |ch, file, sent, total|
104
+ old_file = show_transfer_progress(old_file, file, sent, total)
105
+ }
106
+ else
107
+ ssh.scp.download!(@src_file, @dst_file, {:recursive => @recursive}) { |ch, file, sent, total|
108
+ old_file = show_transfer_progress(old_file, file, sent, total)
109
+ }
110
+ end
111
+ }
112
+ rescue ArgumentError => e
113
+ $stderr.puts e.message
114
+ puts
115
+ usage
116
+ rescue Net::SSH::AuthenticationFailed => e
117
+ $stderr.puts e.message
118
+ rescue Net::SCP::Error => e
119
+ $stderr.puts e.message
120
+ end
121
+
122
+ main(ARGV)
data/bin/direct_ssh CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
- #
4
3
 
5
4
  require 'direct_ssh'
6
5
  require 'net/ssh'
data/bin/simple_ssh ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require 'direct_ssh'
5
+ require 'net/ssh'
6
+
7
+ def usage
8
+ puts 'Usage: simple_ssh user@host [-p port]'
9
+ puts ' - default port 22 will be used if you leave it off'
10
+ end
11
+
12
+ def parse_args(argv)
13
+ raise ArgumentError, 'Invalid argument' unless argv.join(' ') =~ /^([^ ]+)@([^ ]+)( -p ([^ ]+))?$/
14
+ user = $1
15
+ host = $2
16
+ port = ($4 == nil) ? '22' : $4
17
+ [host, user, port]
18
+ end
19
+
20
+ def ssh_exec!(ssh, cmd)
21
+ ssh.exec!(cmd) { |channel, stream, data|
22
+ $stdout.print data.force_encoding("UTF-8") if stream == :stdout
23
+ $stderr.print data.force_encoding("UTF-8") if stream == :stderr
24
+ }
25
+ end
26
+
27
+ def main(argv)
28
+ host, user, port = parse_args(argv)
29
+
30
+ DirectSsh.start(host, user, {:port => port}) { |ssh|
31
+ puts host + ' - ' + ssh.exec!('cat /etc/*-release')
32
+
33
+ input = ''
34
+ until input == 'exit0'
35
+ print '# '
36
+ input = $stdin.gets.chomp
37
+ ssh_exec!(ssh, input) if input != 'exit0'
38
+ end
39
+ }
40
+ rescue ArgumentError => e
41
+ $stderr.puts e.message
42
+ usage
43
+ rescue Net::SSH::AuthenticationFailed => e
44
+ $stderr.puts e.message
45
+ end
46
+
47
+ main(ARGV)
data/direct_ssh.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_runtime_dependency "net-ssh"
24
+ spec.add_runtime_dependency "net-scp"
24
25
  spec.add_runtime_dependency "highline"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.3"
@@ -1,3 +1,3 @@
1
1
  module DirectSsh
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: direct_ssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-03 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: net-scp
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'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: highline
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +96,9 @@ description: Create public/private rsa keys if they do not exist and send public
80
96
  email:
81
97
  - xxjapp@gmail.com
82
98
  executables:
99
+ - direct_scp
83
100
  - direct_ssh
101
+ - simple_ssh
84
102
  extensions: []
85
103
  extra_rdoc_files: []
86
104
  files:
@@ -89,7 +107,9 @@ files:
89
107
  - LICENSE.txt
90
108
  - README.md
91
109
  - Rakefile
110
+ - bin/direct_scp
92
111
  - bin/direct_ssh
112
+ - bin/simple_ssh
93
113
  - direct_ssh.gemspec
94
114
  - lib/direct_ssh.rb
95
115
  - lib/direct_ssh/key_handler.rb