Sha256: 6b411042a44c330b962ebd227d39e1cc321f943c3a29ffe61a6b0be33432ed61

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'net/ssh/stream/version'

require 'net/ssh'

module Net
  module SSH
    class Stream

      def initialize(*args)
        @session ||= Net::SSH.start *args
      end

      def close
        if @session && !@session.closed?
          @session.close
          @session = nil
        end
      end

      def exec(command, stdout: STDOUT, stderr: STDERR)
        exit_code = nil
        
        @session.open_channel do |channel|
          channel.exec(command) do |ch, success|
            unless success
              abort "FAILED: couldn't execute command (ssh.channel.exec)"
            end
            
            channel.on_data do |ch, data|
              stdout << data
            end

            channel.on_extended_data do |ch, type, data|
              stderr << data
            end

            channel.on_request("exit-status") do |ch, data|
              exit_code = data.read_long
            end
          end
        end

        @session.loop
        
        exit_code
      end

      def self.start(*args)
        stream = self.new *args
        yield stream
        stream.close
        nil
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
net-ssh-stream-0.1.0 lib/net/ssh/stream.rb