Class: Remote::Session
- Inherits:
-
Object
- Object
- Remote::Session
- 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
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#private_key ⇒ Object
Returns the value of attribute private_key.
-
#prompts ⇒ Object
Returns the value of attribute prompts.
-
#session ⇒ Object
Returns the value of attribute session.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(host, options = {}) ⇒ Session
constructor
A new instance of Session.
- #put(remote_path, &block) ⇒ Object
- #run(command) ⇒ Object
- #sudo(commands) ⇒ Object
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, = {} ) @session = nil @host = host @username = [ :username ] || ENV[ 'USER' ] @password = [ :password ] @port = [ :port ] @private_key = [ :private_key ] @prompts = [ :prompts ] || {} @sudo_password = [ :sudo_password ] connect end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
23 24 25 |
# File 'lib/remote/session.rb', line 23 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
25 26 27 |
# File 'lib/remote/session.rb', line 25 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
26 27 28 |
# File 'lib/remote/session.rb', line 26 def port @port end |
#private_key ⇒ Object
Returns the value of attribute private_key.
27 28 29 |
# File 'lib/remote/session.rb', line 27 def private_key @private_key end |
#prompts ⇒ Object
Returns the value of attribute prompts.
28 29 30 |
# File 'lib/remote/session.rb', line 28 def prompts @prompts end |
#session ⇒ Object
Returns the value of attribute session.
29 30 31 |
# File 'lib/remote/session.rb', line 29 def session @session end |
#username ⇒ Object
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, = {}, &block ) rs = new( host, ) block.call rs rs.close end |
Instance Method Details
#close ⇒ Object
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 |