Class: Remote::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/remote/session.rb,
lib/remote/session/send.rb,
lib/remote/session/version.rb,
lib/remote/session/send_file.rb,
lib/remote/session/send_string.rb

Defined Under Namespace

Classes: Send, SendFile, SendString

Constant Summary collapse

SUDO_PASSWORD_PROMPT =
'remote-session-sudo-prompt'
ROOT_COMMAND_PROMPT =
'remote-session-prompt#'
ROOT_COMMAND_PROMPT_MATCH =
/#{ ROOT_COMMAND_PROMPT }$/
MAJOR =
0
MINOR =
0
REVISION =
6
VERSION =
[ MAJOR, MINOR, REVISION ].map( &:to_s ).join( '.' )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ Session

Returns a new instance of Session.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/remote/session.rb', line 31

def initialize( host, options = {} )
  @session       = nil
  @host          = host
  @username      = options[ :username      ] || ENV[ 'USER' ]
  @password      = options[ :password      ]
  @port          = options[ :port          ]
  @private_key   = options[ :private_key   ]
  @prompts       = options[ :prompts       ] || {}
  @sudo_password = options[ :sudo_password ]

  connect
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#passwordObject

Returns the value of attribute password.



25
26
27
# File 'lib/remote/session.rb', line 25

def password
  @password
end

#portObject

Returns the value of attribute port.



26
27
28
# File 'lib/remote/session.rb', line 26

def port
  @port
end

#private_keyObject

Returns the value of attribute private_key.



27
28
29
# File 'lib/remote/session.rb', line 27

def private_key
  @private_key
end

#promptsObject

Returns the value of attribute prompts.



28
29
30
# File 'lib/remote/session.rb', line 28

def prompts
  @prompts
end

#sessionObject

Returns the value of attribute session.



29
30
31
# File 'lib/remote/session.rb', line 29

def session
  @session
end

#usernameObject

Returns the value of attribute username.



24
25
26
# File 'lib/remote/session.rb', line 24

def username
  @username
end

Class Method Details

.open(host, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/remote/session.rb', line 15

def self.open( host, options = {}, &block )
  rs = new( host, options )

  block.call rs

  rs.close
end

Instance Method Details

#closeObject



64
65
66
67
# File 'lib/remote/session.rb', line 64

def close
  @session.close
  @session = nil
end

#put(remote_path, &block) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/remote/session.rb', line 69

def put( remote_path, &block )
  sftp = Net::SFTP::Session.new( @session ).connect!
  sftp.file.open( remote_path, 'w' ) do |f|
    f.write block.call
  end
  sftp.close_channel
end

#run(command) ⇒ Object



44
45
46
47
48
# File 'lib/remote/session.rb', line 44

def run( command )
  raise "Session is closed" if @session.nil?
  puts "@#{ @host }: #{ command }"
  puts @session.exec!( command )
end

#sudo(commands) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/remote/session.rb', line 50

def sudo( commands )
  raise "Session is closed" if @session.nil?
  commands = [ *commands ]

  @session.open_channel do |ch|
    ch.request_pty do |ch, success|
      raise "Could not obtain pty" if ! success

      channel_exec ch, commands
    end
  end
  @session.loop
end