Class: HereOrThere::Remote::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/here_or_there/remote.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SSH

Returns a new instance of SSH.



25
26
27
28
29
# File 'lib/here_or_there/remote.rb', line 25

def initialize options
  @options   = options.dup
  @hostname  = @options.delete(:hostname)
  @user      = @options.delete(:user)
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



22
23
24
# File 'lib/here_or_there/remote.rb', line 22

def hostname
  @hostname
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/here_or_there/remote.rb', line 22

def options
  @options
end

#sessionObject (readonly)

Returns the value of attribute session.



23
24
25
# File 'lib/here_or_there/remote.rb', line 23

def session
  @session
end

#userObject (readonly)

Returns the value of attribute user.



22
23
24
# File 'lib/here_or_there/remote.rb', line 22

def user
  @user
end

Instance Method Details

#close_sessionObject



54
55
56
# File 'lib/here_or_there/remote.rb', line 54

def close_session
  session.close unless !session || session.closed?
end

#run(command) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/here_or_there/remote.rb', line 31

def run command
  stdout, stderr, status = [ '', '', true ]

  open_session

  session.exec! command do |channel, response_type, response_data|

    if response_type == :stdout
      stdout << response_data
    else
      stderr << response_data
      status = false
    end

  end

  return Response.new( stdout, stderr, status )

rescue Net::SSH::AuthenticationFailed
  close_session
  return Response.new( '', 'Authentication failed when connecting to remote', false )
end