Class: Net::SSH::Simple
- Inherits:
-
Object
- Object
- Net::SSH::Simple
- Includes:
- Blockenspiel::DSL
- Defined in:
- lib/net/ssh/simple.rb,
lib/net/ssh/simple.rb,
lib/net/ssh/simple/version.rb
Overview
Net::SSH::Simple is a simple wrapper around Net::SSH and Net::SCP.
Defined Under Namespace
Constant Summary
- MAX_TIMEOUT =
is_64bit_platform ? 2**32 : 2**16
- VERSION =
"1.7.0"
Instance Attribute Summary (collapse)
-
- (Net::SSH::Simple::Result) result
readonly
Result of the current Net::SSH::Simple::Operation.
Class Method Summary (collapse)
-
+ (Thread) async(opts = {}, &block)
Spawn a Thread to perform a sequence of ssh/scp operations.
-
+ (Net::SSH::Simple::Result) scp_get(*args, &block)
SCP download from a remote host.
-
+ (Net::SSH::Simple::Result) scp_put(*args, &block)
SCP upload to a remote host.
-
+ (Net::SSH::Simple::Result) ssh(*args, &block)
Perform ssh command on a remote host and capture the result.
-
+ (Net::SSH::Simple::Result) sync(opts = {}, &block)
Perform a sequence of ssh/scp operations.
Instance Method Summary (collapse)
-
- (Thread) async(opts = {}, &block)
Spawn a Thread to perform a sequence of ssh/scp operations.
-
- (Net::SSH::Simple::Result) close
Close and cleanup.
-
- (Simple) initialize(opts = {})
constructor
A new instance of Simple.
-
- (Net::SSH::Simple::Result) scp_get(host, src, dst, opts = {}, &block)
SCP download from a remote host.
-
- (Net::SSH::Simple::Result) scp_put(host, src, dst, opts = {}, &block)
SCP upload to a remote host.
-
- (Net::SSH::Simple::Result) ssh(host, cmd, opts = {}, &block)
Perform SSH operation on a remote host and capture the result.
Constructor Details
- (Simple) initialize(opts = {})
Returns a new instance of Simple
538 539 540 541 542 |
# File 'lib/net/ssh/simple.rb', line 538 def initialize(opts={}) @opts = opts Thread.current[:ssh_simple_sessions] = {} @result = Result.new end |
Instance Attribute Details
- (Net::SSH::Simple::Result) result (readonly)
Result of the current Net::SSH::Simple::Operation.
253 254 255 |
# File 'lib/net/ssh/simple.rb', line 253 def result @result end |
Class Method Details
+ (Thread) async(opts = {}, &block)
Spawn a Thread to perform a sequence of ssh/scp operations.
551 552 553 554 555 |
# File 'lib/net/ssh/simple.rb', line 551 def self.async(opts={}, &block) Thread.new do self.sync(opts, &block) end end |
+ (Net::SSH::Simple::Result) scp_get(*args, &block)
SCP download from a remote host. This will create a new connection for each invocation.
317 318 319 320 321 322 |
# File 'lib/net/ssh/simple.rb', line 317 def self.scp_get(*args, &block) s = self.new r = s.scp_get(*args, &block) s.close r end |
+ (Net::SSH::Simple::Result) scp_put(*args, &block)
SCP upload to a remote host. This will create a new connection for each invocation.
292 293 294 295 296 297 |
# File 'lib/net/ssh/simple.rb', line 292 def self.scp_put(*args, &block) s = self.new r = s.scp_put(*args, &block) s.close r end |
+ (Net::SSH::Simple::Result) ssh(*args, &block)
Perform ssh command on a remote host and capture the result. This will create a new connection for each invocation.
268 269 270 271 272 273 |
# File 'lib/net/ssh/simple.rb', line 268 def self.ssh(*args, &block) s = self.new r = s.ssh(*args, &block) s.close r end |
+ (Net::SSH::Simple::Result) sync(opts = {}, &block)
Perform a sequence of ssh/scp operations.
575 576 577 578 579 580 |
# File 'lib/net/ssh/simple.rb', line 575 def self.sync(opts={}, &block) s = self.new(opts) r = Blockenspiel.invoke(block, s) s.close r end |
Instance Method Details
- (Thread) async(opts = {}, &block)
Spawn a Thread to perform a sequence of ssh/scp operations.
564 565 566 567 |
# File 'lib/net/ssh/simple.rb', line 564 def async(opts={}, &block) opts = @opts.merge(opts) self.class.async(opts, &block) end |
- (Net::SSH::Simple::Result) close
Close and cleanup.
587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/net/ssh/simple.rb', line 587 def close Thread.current[:ssh_simple_sessions].values.each do |session| begin ::Timeout.timeout(@opts[:close_timeout] || 5) { session.close } rescue => e begin session.shutdown! rescue end end end @result end |
- (Net::SSH::Simple::Result) scp_get(host, src, dst, opts = {}, &block)
SCP download from a remote host. The underlying Net::SSH::Simple instance will re-use existing connections for optimal performance.
354 355 356 357 |
# File 'lib/net/ssh/simple.rb', line 354 def scp_get(host, src, dst, opts={}, &block) opts = @opts.merge(opts) scp(:download, host, src, dst, opts, &block) end |
- (Net::SSH::Simple::Result) scp_put(host, src, dst, opts = {}, &block)
SCP upload to a remote host. The underlying Net::SSH::Simple instance will re-use existing connections for optimal performance.
336 337 338 339 |
# File 'lib/net/ssh/simple.rb', line 336 def scp_put(host, src, dst, opts={}, &block) opts = @opts.merge(opts) scp(:upload, host, src, dst, opts, &block) end |
- (Net::SSH::Simple::Result) ssh(host, cmd, opts = {}, &block)
Perform SSH operation on a remote host and capture the result. The underlying Net::SSH::Simple instance will re-use existing connections for optimal performance.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/net/ssh/simple.rb', line 488 def ssh(host, cmd, opts={}, &block) opts = @opts.merge(opts) with_session(host, opts) do |session| @result = Result.new( { :op => :ssh, :host => host, :cmd => cmd, :start_at => Time.new, :last_event_at => Time.new, :opts => opts, :stdout => '', :stderr => '', :success => nil } ) channel = session.open_channel do |chan| chan.exec cmd do |ch, success| @result[:success] = success ch.on_data do |c, data| @result[:last_event_at] = Time.new r = block.call(:stdout, ch, data) if block @result[:stdout] += data.to_s unless r == :no_append end ch.on_extended_data do |c, type, data| @result[:last_event_at] = Time.new r = block.call(:stderr, ch, data) if block @result[:stderr] += data.to_s unless r == :no_append end ch.on_request('exit-status') do |c, data| @result[:last_event_at] = Time.new exit_code = data.read_long block.call(:exit_code, ch, exit_code) if block @result[:exit_code] = exit_code end ch.on_request('exit-signal') do |c, data| @result[:last_event_at] = Time.new exit_signal = data.read_string r = block.call(:exit_signal, ch, exit_signal) if block @result[:exit_signal] = exit_signal @result[:success] = false unless r == :no_raise raise "Killed by SIG#{@result[:exit_signal]}" end end block.call(:start, ch, nil) if block end end wait_for_channel session, channel, @result, opts @result[:finish_at] = Time.new block.call(:finish, channel, nil) if block @result end end |