Class SSH
In: lib/svengali/ssh.rb
Parent: Object

Methods

Attributes

session  [R] 
sftp_session  [R] 

Public Class methods

[Source]

# File lib/svengali/ssh.rb, line 19
  def initialize(host,user_name=nil,password=nil,private_key_path=nil)
    if user_name && password && private_key_path
      @user_name = user_name
      @passphrase = password
      @private_key_path = private_key_path
    elsif user_name || password
      @user_name = user_name
      @password = password
      initialize(host)
    else #substitute of constructor which has no argument
      @host = host
      @terminator = ""

      wait_until_ssh_connectable(CLibIPAddr.new(host))

      if @user_name && @password && @private_key_path #if initialized with private_key_path, user_name and password
        debug_p "Try Net::SSH.start()!"
        @session = Net::SSH.start(@host, @user_name,:passphrase => @passphrase, :keys => [ @private_key_path ] )
      elsif @user_name && @password #if initialized with user_name and password
        debug_p "Try Net::SSH.start()!"
        @session = Net::SSH.start(@host, @user_name,:password => @password)
      elsif @user_name #if initialized with user_name
        debug_p "Try Net::SSH.start()!"        
        @session = Net::SSH.start(@host, @user_name)
      end
      debug_p "Net::SSH.start finished."
      @sftp_session = @session.sftp.connect()
    end
  end

Public Instance methods

[Source]

# File lib/svengali/ssh.rb, line 49
  def close
    @session.close()
  end

return value : nothing

[Source]

# File lib/svengali/ssh.rb, line 82
  def exec(command_str)
    debug_p "exec -> \"#{command_str}\""

    @session.exec(command_str)
  end

return value : String of stdout or stderr

[Source]

# File lib/svengali/ssh.rb, line 58
  def exec!(command_str,time_out = nil)
    debug_p "exec! -> \"#{command_str}\""

      begin
        timeout(time_out) do
          return_val = ""
          @session.exec!(command_str) { |ch, stream, data|
            if stream == :stdout
              return_val += data
            elsif stream == :stderr
              return_val += data
            end
          }
          return return_val
        end
      rescue TimeoutError
        return nil
      end
  end

[Source]

# File lib/svengali/ssh.rb, line 88
  def wait_until_ssh_connectable(host)
      # waits until ensure that ssh connection can be accept
      while(is_ssh_connectable(host) == false)
        sleep(1)
      end
  end

[Validate]