direct_ssh 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3df4c18561f99a4f575ae461e874957d96520c9ddaae0b74b31efdb1adcbb316
4
- data.tar.gz: 6c3d0c66f70e48f87cf0b7463ddb7d83a214c496aa8a82fb95bd3ca78768cacb
3
+ metadata.gz: 0f830d2885afb47efdda1ab0fb71717a1a520e8a1862c4c9091062b938e0e386
4
+ data.tar.gz: a047bd0d2a74fcb96bf5a9383fb57b0b42a977ce5725d10ca7e856cc16beb106
5
5
  SHA512:
6
- metadata.gz: fb0d6ec9537203786cd033a02d047c24d314236fd7ce0b7d99c7e1e78987168bafc85301331b35fcf7d71b4ce99ec6cb557c240666c42d597362a053eb304af2
7
- data.tar.gz: c1ed0cf88bde4eb7a86e0f920bd83941d374a847483003ab66fd03ede2c0a82d62625c437560fb683a150e5a20ab0d058ac063d9aa5fbeca857ec705f47fbb76
6
+ metadata.gz: ec3fed44a476fa188d80941efdeb6b229852355764d00384d8c3eb178640448521a69a75e68ec2b243e1bde4f8f7b08c1a38104394bb01ac2f759123c5d6eae4
7
+ data.tar.gz: 0fa20e65151e8a2796aec8303d4e09d78e5f44e1bd113c0e6d63b089dfcbbfadd6a3a9a8b06b78cc808470870a56adb468070cdfd7f85af29aca68effce5face
File without changes
@@ -6,7 +6,7 @@ require 'base64'
6
6
 
7
7
  module KeyHandler
8
8
  def self.send_key_to_remote(ssh)
9
- ssh_public_key = get_ssh_public_key
9
+ ssh_public_key = get_ssh_public_key.chomp
10
10
  send_ssh_public_key_to_remote(ssh, ssh_public_key)
11
11
  end
12
12
 
@@ -32,11 +32,11 @@ module KeyHandler
32
32
  end
33
33
 
34
34
  def self.create_ssh_files
35
- FileUtils.mkdir_p Dir.home + '/.ssh'
36
- FileUtils.touch Dir.home + '/.ssh/id_rsa'
37
- FileUtils.touch Dir.home + '/.ssh/id_rsa.pub'
38
- FileUtils.touch Dir.home + '/.ssh/authorized_keys'
39
- FileUtils.touch Dir.home + '/.ssh/known_hosts'
35
+ FileUtils.mkdir Dir.home + '/.ssh'
36
+ FileUtils.touch Dir.home + '/.ssh/id_rsa'
37
+ FileUtils.touch Dir.home + '/.ssh/id_rsa.pub'
38
+ FileUtils.touch Dir.home + '/.ssh/authorized_keys'
39
+ FileUtils.touch Dir.home + '/.ssh/known_hosts'
40
40
  end
41
41
 
42
42
  # see: http://www.noah.org/wiki/SSH_public_keys
@@ -61,34 +61,73 @@ module KeyHandler
61
61
  ################################################################
62
62
  # remote ssh key process
63
63
 
64
+ def self.is_windows?(ssh)
65
+ ssh.exec!("echo %os%").chomp != "%os%"
66
+ end
67
+
64
68
  def self.send_ssh_public_key_to_remote(ssh, key)
69
+ is_win = is_windows?(ssh)
70
+
65
71
  if !remote_file_exists?(ssh, '~/.ssh/authorized_keys')
66
- remote_create_ssh_files(ssh)
67
- remote_chmod_ssh_files(ssh)
72
+ remote_create_ssh_files(ssh, is_win)
73
+ remote_chmod_ssh_files(ssh, is_win)
68
74
  end
69
75
 
70
- # append public_key to remote '~/.ssh/authorized_keys'
71
- ssh.exec!("echo '#{key}' >> ~/.ssh/authorized_keys")
76
+ remote_append_key(ssh, key, is_win)
72
77
  end
73
78
 
74
79
  def self.remote_file_exists?(ssh, path)
75
- ssh.exec!("[ ! -f #{path} ] && echo NOT_EXIST") == nil
80
+ # windows & linux OK
81
+ # path including '~/' OK
82
+ ssh.exec!("[ ! -f #{path} ] && echo NOT_EXIST").empty?
76
83
  end
77
84
 
78
- def self.remote_create_ssh_files(ssh)
79
- ssh.exec!('mkdir -p ~/.ssh')
80
- ssh.exec!('touch ~/.ssh/id_rsa')
81
- ssh.exec!('touch ~/.ssh/id_rsa.pub')
82
- ssh.exec!('touch ~/.ssh/authorized_keys')
83
- ssh.exec!('touch ~/.ssh/known_hosts')
85
+ def self.remote_create_ssh_files(ssh, is_win)
86
+ if is_win
87
+ ssh_exec!(ssh, 'mkdir .ssh')
88
+ ssh_exec!(ssh, 'touch .ssh\id_rsa')
89
+ ssh_exec!(ssh, 'touch .ssh\id_rsa.pub')
90
+ ssh_exec!(ssh, 'touch .ssh\authorized_keys')
91
+ ssh_exec!(ssh, 'touch .ssh\known_hosts')
92
+ else
93
+ ssh_exec!(ssh, 'mkdir ~/.ssh')
94
+ ssh_exec!(ssh, 'touch ~/.ssh/id_rsa')
95
+ ssh_exec!(ssh, 'touch ~/.ssh/id_rsa.pub')
96
+ ssh_exec!(ssh, 'touch ~/.ssh/authorized_keys')
97
+ ssh_exec!(ssh, 'touch ~/.ssh/known_hosts')
98
+ end
84
99
  end
85
100
 
86
101
  # see: http://www.noah.org/wiki/SSH_public_keys
87
- def self.remote_chmod_ssh_files(ssh)
88
- ssh.exec!('chmod 700 ~/.ssh')
89
- ssh.exec!('chmod 600 ~/.ssh/id_rsa')
90
- ssh.exec!('chmod 644 ~/.ssh/id_rsa.pub')
91
- ssh.exec!('chmod 644 ~/.ssh/authorized_keys')
92
- ssh.exec!('chmod 644 ~/.ssh/known_hosts')
102
+ def self.remote_chmod_ssh_files(ssh, is_win)
103
+ if is_win
104
+ # puts "NOTE 1: The default mode on windows should work"
105
+ # puts "NOTE 2: 'chmod' is not available or doesn't work on windows."
106
+ # puts " If password asked, try to handle according to"
107
+ # puts " https://social.technet.microsoft.com/Forums/Azure/en-US/e4c11aed-1d8b-4ff4-89ad-c90c62e13ce0/ssh-asking-for-password-even-i-have-private-key"
108
+ # puts " and log file C:\\ProgramData\\ssh\\logs\\sshd.log"
109
+ else
110
+ ssh_exec!(ssh, 'chmod 700 ~/.ssh')
111
+ ssh_exec!(ssh, 'chmod 600 ~/.ssh/id_rsa')
112
+ ssh_exec!(ssh, 'chmod 644 ~/.ssh/id_rsa.pub')
113
+ ssh_exec!(ssh, 'chmod 644 ~/.ssh/authorized_keys')
114
+ ssh_exec!(ssh, 'chmod 644 ~/.ssh/known_hosts')
115
+ end
116
+ end
117
+
118
+ # append public_key to remote '~/.ssh/authorized_keys'
119
+ def self.remote_append_key(ssh, key, is_win)
120
+ if is_win
121
+ ssh_exec!(ssh, "echo #{key} >> .ssh\\authorized_keys")
122
+ else
123
+ ssh_exec!(ssh, "echo #{key} >> ~/.ssh/authorized_keys")
124
+ end
125
+ end
126
+
127
+ def self.ssh_exec!(ssh, cmd)
128
+ # puts "# #{cmd}"
129
+ res = ssh.exec! cmd
130
+ # puts res.force_encoding('SJIS').encode('UTF-8')
131
+ res
93
132
  end
94
133
  end
@@ -1,3 +1,3 @@
1
1
  module DirectSsh
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: direct_ssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xia Xiongjun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-16 00:00:00.000000000 Z
11
+ date: 2019-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh