Class: HrrRbSsh::Client

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/hrr_rb_ssh/client.rb

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Constructor Details

#initialize(target, tmp_options = {}) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hrr_rb_ssh/client.rb', line 30

def initialize target, tmp_options={}
  @closed = true
  options = initialize_options tmp_options
  io = case target
       when IO
         target
       when Array
         io = TCPSocket.new *target
       when String
         port = 22
         io = TCPSocket.new target, port
       end
  transport      = HrrRbSsh::Transport.new      io, HrrRbSsh::Mode::CLIENT, options, logger: logger
  authentication = HrrRbSsh::Authentication.new transport, HrrRbSsh::Mode::CLIENT, options, logger: logger
  @connection    = HrrRbSsh::Connection.new     authentication, HrrRbSsh::Mode::CLIENT, options, logger: logger
end

Class Method Details

.start(target, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hrr_rb_ssh/client.rb', line 16

def self.start target, options={}
  client = self.new target, options
  client.start
  if block_given?
    begin
      yield client
    ensure
      client.close unless client.closed?
    end
  else
    client
  end
end

Instance Method Details

#closeObject



77
78
79
80
81
82
# File 'lib/hrr_rb_ssh/client.rb', line 77

def close
  log_debug { "closing client" }
  @closed = true
  @connection.close
  log_debug { "client closed" }
end

#closed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/hrr_rb_ssh/client.rb', line 73

def closed?
  @closed
end

#exec(command, pty: false, env: {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hrr_rb_ssh/client.rb', line 143

def exec command, pty: false, env: {}
  log_debug { "start exec: #{command}" }
  begin
    log_info { "Opning channel" }
    channel = @connection.request_channel_open "session"
    log_info { "Channel opened" }
    if pty
      channel.send_channel_request_pty_req 'xterm', 80, 24, 580, 336, ''
    end
    env.each{ |variable_name, variable_value|
      channel.send_channel_request_env variable_name, variable_value
    }
    channel.send_channel_request_exec command
    yield channel.io
  rescue => e
    log_error { "Failed opening channel" }
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    raise "Error in shell"
  ensure
    if channel
      log_info { "closing channel IOs" }
      channel.io.each{ |io| io.close rescue nil }
      log_info { "channel IOs closed" }
      log_info { "closing channel" }
      log_info { "wait until threads closed in channel" }
      channel.wait_until_closed
      channel.close
      log_info { "channel closed" }
    end
  end
  channel_exit_status = channel.exit_status rescue nil
end

#exec!(command, pty: false, env: {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hrr_rb_ssh/client.rb', line 84

def exec! command, pty: false, env: {}
  log_debug { "start exec!: #{command}" }
  out_buf = StringIO.new
  err_buf = StringIO.new
  begin
    log_info { "Opning channel" }
    channel = @connection.request_channel_open "session"
    log_info { "Channel opened" }
    if pty
      channel.send_channel_request_pty_req 'xterm', 80, 24, 580, 336, ''
    end
    env.each{ |variable_name, variable_value|
      channel.send_channel_request_env variable_name, variable_value
    }
    channel.send_channel_request_exec command
    out_t = Thread.new {
      while true
        begin
          out_buf.write channel.io[1].readpartial(10240)
        rescue
          break
        end
      end
    }
    err_t = Thread.new {
      while true
        begin
          err_buf.write channel.io[2].readpartial(10240)
        rescue
          break
        end
      end
    }
    [out_t, err_t].each{ |t|
      begin
        t.join
      rescue => e
        log_warn { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
      end
    }
  rescue => e
    log_error { "Failed opening channel" }
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    raise "Error in exec!"
  ensure
    if channel
      log_info { "closing channel IOs" }
      channel.io.each{ |io| io.close rescue nil }
      log_info { "channel IOs closed" }
      log_info { "closing channel" }
      log_info { "wait until threads closed in channel" }
      channel.wait_until_closed
      channel.close
      log_info { "channel closed" }
    end
  end
  [out_buf.string, err_buf.string]
end

#initialize_options(tmp_options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hrr_rb_ssh/client.rb', line 47

def initialize_options tmp_options
  tmp_options = Hash[tmp_options.map{|k, v| [k.to_s, v]}]
  self.logger = tmp_options['logger']
  options = {}
  options['username'] = tmp_options['username']
  options['authentication_preferred_authentication_methods'] = tmp_options['authentication_preferred_authentication_methods']
  options['client_authentication_password']                  = tmp_options['password']
  options['client_authentication_publickey']                 = tmp_options['publickey']
  options['client_authentication_keyboard_interactive']      = tmp_options['keyboard_interactive']
  options['transport_preferred_encryption_algorithms']       = tmp_options['transport_preferred_encryption_algorithms']
  options['transport_preferred_server_host_key_algorithms']  = tmp_options['transport_preferred_server_host_key_algorithms']
  options['transport_preferred_kex_algorithms']              = tmp_options['transport_preferred_kex_algorithms']
  options['transport_preferred_mac_algorithms']              = tmp_options['transport_preferred_mac_algorithms']
  options['transport_preferred_compression_algorithms']      = tmp_options['transport_preferred_compression_algorithms']
  options
end

#loopObject



69
70
71
# File 'lib/hrr_rb_ssh/client.rb', line 69

def loop
  @connection.loop
end

#shell(env: {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/hrr_rb_ssh/client.rb', line 176

def shell env: {}
  log_debug { "start shell" }
  begin
    log_info { "Opning channel" }
    channel = @connection.request_channel_open "session"
    log_info { "Channel opened" }
    channel.send_channel_request_pty_req 'xterm', 80, 24, 580, 336, ''
    env.each{ |variable_name, variable_value|
      channel.send_channel_request_env variable_name, variable_value
    }
    channel.send_channel_request_shell
    yield channel.io
  rescue => e
    log_error { "Failed opening channel" }
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    raise "Error in shell"
  ensure
    if channel
      log_info { "closing channel IOs" }
      channel.io.each{ |io| io.close rescue nil }
      log_info { "channel IOs closed" }
      log_info { "closing channel" }
      log_info { "wait until threads closed in channel" }
      channel.wait_until_closed
      channel.close
      log_info { "channel closed" }
    end
  end
  channel_exit_status = channel.exit_status rescue nil
end

#startObject



64
65
66
67
# File 'lib/hrr_rb_ssh/client.rb', line 64

def start
  @connection.start foreground: false
  @closed = false
end

#subsystem(name) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/hrr_rb_ssh/client.rb', line 207

def subsystem name
  log_debug { "start subsystem" }
  begin
    log_info { "Opning channel" }
    channel = @connection.request_channel_open "session"
    log_info { "Channel opened" }
    channel.send_channel_request_subsystem name
    yield channel.io
  rescue => e
    log_error { "Failed opening channel" }
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    raise "Error in subsystem"
  ensure
    if channel
      log_info { "closing channel IOs" }
      channel.io.each{ |io| io.close rescue nil }
      log_info { "channel IOs closed" }
      log_info { "closing channel" }
      log_info { "wait until threads closed in channel" }
      channel.wait_until_closed
      channel.close
      log_info { "channel closed" }
    end
  end
  channel_exit_status = channel.exit_status rescue nil
end