# class GxG::Networking::SshClient

SSH Client

### Public Class Methods

new() click to toggle source

    # File lib/gxg/net_clients.rb, line 4138
    def initialize()
      @client = nil
      self
    end

### Public Instance Methods

async_download(the_remote_source=nil, the_destination=nil, options={}, &progress_block) click to toggle source

    # File lib/gxg/net_clients.rb, line 4271
    def async_download(the_remote_source=nil, the_destination=nil, options={}, &progress_block)
      result = false
      begin
        if @client
          unless the_remote_source.is_a?(::String)
            raise ArgumentError, "You MUST provide a local path as a String."
          end
          unless the_destination.is_a?(::String)
            raise ArgumentError, "You MUST provide a remote path as a String."
          end
          if progress_block.respond_to?(:call)
            channel = @client.scp.download(the_remote_source, the_destination, options, &progress_block)
          else
            channel = @client.scp.download(the_remote_source, the_destination, options)
          end
          channel.wait
          # True result means the request was sent, not that it completed.
          result = true
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:remote_path => the_remote_source, :local_path => the_destination, :options => options}})
      end
      result
    end

async_upload(the_local_source=nil, the_destination=nil, options={}, &progress_block) click to toggle source

    # File lib/gxg/net_clients.rb, line 4214
    def async_upload(the_local_source=nil, the_destination=nil, options={}, &progress_block)
      result = false
      begin
        if @client
          unless the_local_source.is_a?(::String)
            raise ArgumentError, "You MUST provide a local path as a String."
          end
          unless the_destination.is_a?(::String)
            raise ArgumentError, "You MUST provide a remote path as a String."
          end
          if progress_block.respond_to?(:call)
            channel = @client.scp.upload(the_local_source, the_destination, options, &progress_block)
          else
            channel = @client.scp.upload(the_local_source, the_destination, options)
          end
          channel.wait
          # True result means the request was sent, not that it completed.
          result = true
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:local_path => the_local_source, :remote_path => the_destination, :options => options}})
      end
      result
    end

channel_operations(&block) click to toggle source

    # File lib/gxg/net_clients.rb, line 4176
    def channel_operations(&block)
      result = nil
      begin
        if @client
          unless block.respond_to?(:call)
            raise ArgumentError, "You MUST provide a valid Block/Proc to execute."
          end
          result = @client.open_channel(&block)
        end
        #
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:block => block}})
      end
      result
    end

closed?() click to toggle source

    # File lib/gxg/net_clients.rb, line 4206
    def closed?()
      if @client
        false
      else
        true
      end
    end

download(the_remote_source=nil, the_destination=nil, options={}, &progress_block) click to toggle source

    # File lib/gxg/net_clients.rb, line 4296
    def download(the_remote_source=nil, the_destination=nil, options={}, &progress_block)
      # options:
      #:recursive - the local parameter refers to a local directory, which should be uploaded to a new directory named remote
      # on the remote server.
      #
      #:preserve - the atime and mtime of the file should be preserved.
      #
      #:verbose - the process should result in verbose output on the server end (useful for debugging).
      #
      result = false
      begin
        if @client
          unless the_remote_source.is_a?(::String)
            raise ArgumentError, "You MUST provide a remote path as a String."
          end
          unless the_destination.is_a?(::String)
            raise ArgumentError, "You MUST provide a local path (or file name) as a String."
          end
          if progress_block.respond_to?(:call)
            result = @client.scp.download!(the_remote_source, the_destination, options, &progress_block)
          else
            result = @client.scp.download!(the_remote_source, the_destination, options)
          end
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:remote_path => the_remote_source, :local_path => the_destination, :options => options}})
      end
      result
    end

login(the_url=nil, options={}) click to toggle source

    # File lib/gxg/net_clients.rb, line 4143
    def login(the_url=nil, options={})
      result = false
      begin
        unless the_url.is_a?(::URI::Generic)
          raise ArgumentError, "You MUST provide a valid URI"
        end
        if the_url.password
          options[:password] = the_url.password
        end
        @client = ::Net::SSH.start(the_url.hostname, the_url.user, options)
        result = true
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
      end
      result
    end

logout() click to toggle source

    # File lib/gxg/net_clients.rb, line 4192
    def logout()
      result = false
      begin
        if @client
          @client.exec!("exit")
          @client = nil
          result = true
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {}})
      end
      result
    end

send_command(the_command=nil) click to toggle source

    # File lib/gxg/net_clients.rb, line 4160
    def send_command(the_command=nil)
      result = nil
      begin
        if @client
          unless the_command.is_a?(::String)
            raise ArgumentError, "You MUST provide a command as a String."
          end
          result = @client.exec!(the_command)
        end
        #
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:command => the_command}})
      end
      result
    end

upload(the_local_source=nil, the_destination=nil, options={}, &progress_block) click to toggle source

    # File lib/gxg/net_clients.rb, line 4239
    def upload(the_local_source=nil, the_destination=nil, options={}, &progress_block)
      # options:
      #:recursive - the local parameter refers to a local directory, which should be uploaded to a new directory named remote
      # on the remote server.
      #
      #:preserve - the atime and mtime of the file should be preserved.
      #
      #:verbose - the process should result in verbose output on the server end (useful for debugging).
      #
      #:chunk_size - the size of each "chunk" that should be sent. Defaults to 2048. Changing this value may
      # improve throughput at the expense of decreasing interactivity.
      result = false
      begin
        if @client
          unless the_local_source.is_a?(::String)
            raise ArgumentError, "You MUST provide a local path as a String."
          end
          unless the_destination.is_a?(::String)
            raise ArgumentError, "You MUST provide a remote path as a String."
          end
          if progress_block.respond_to?(:call)
            result = @client.scp.upload!(the_local_source, the_destination, options, &progress_block)
          else
            result = @client.scp.upload!(the_local_source, the_destination, options)
          end
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:local_path => the_local_source, :remote_path => the_destination, :options => options}})
      end
      result
    end