A delegate class to manage the processing for a single executed process on the remote host. It opens a channel, executes the process on it, and manages the various callbacks.
This service is typically used like this:
Net::SSH.start( 'host', 'user' ) do |session| session.process.open( "bc" ) do |process| ... end end
- close
- close_input
- do_close
- do_confirm
- do_data
- do_exec_failure
- do_exec_success
- do_extended_data
- do_request
- new
- on_exit
- on_failure
- on_stderr
- on_stdout
- on_success
- puts
- write
Create a new OpenManager instance on the given connection. It will attempt to execute the given command. If a block is given, the manager will be yielded to the block, and the constructor will not return until all channels are closed.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 41 41: def initialize( connection, log, command ) 42: @log = log 43: @command = command 44: @channel = connection.open_channel( 45: "session", &method( :do_confirm ) ) 46: 47: if block_given? 48: yield self 49: connection.loop 50: end 51: end
Close the process. All streams (stdin, stdout, stderr) will be closed. Any output that the process had already produced will still be sent, but it will be shut down as soon as possible. This will return immediately.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 122 122: def close 123: @channel.close 124: end
Indicate that no more data will be sent to the process (sends an EOF to the process). The process may continue to send data, but the stdin stream is effectively closed. This will return immediately.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 114 114: def close_input 115: @channel.send_eof 116: end
Invoked when the channel is closed. This simply delegates to the on_exit callback, if registered.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 176 176: def do_close( channel ) 177: @on_exit.call( self, @exit_status ) if @on_exit 178: end
Invoked when the channel‘s opening has been confirmed by the server. This is where the command to execute will be sent to the server.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 129 129: def do_confirm( channel ) 130: channel.on_success &method(:do_exec_success) 131: channel.on_failure &method(:do_exec_failure) 132: channel.exec @command, true 133: end
Invoked when data arrives over the channel. This simply delegates to the on_stdout callback, if registered.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 160 160: def do_data( channel, data ) 161: @on_stdout.call( self, data ) if @on_stdout 162: end
Invoked when the invocation of the command failed. This will call the on_failure callback, if registered, or will otherwise raise an exception.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 149 149: def do_exec_failure( channel ) 150: if @on_failure 151: @on_failure.call( self, nil ) 152: else 153: raise Net::SSH::Exception, 154: "could not execute process (#{@command})" 155: end 156: end
Invoked when the invocation of the command has been successful. This registers various callbacks, and then calls the on_success callback (if registered).
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 138 138: def do_exec_success( channel ) 139: channel.on_data &method(:do_data) 140: channel.on_extended_data &method(:do_extended_data) 141: channel.on_close &method(:do_close) 142: channel.on_request &method(:do_request) 143: @on_success.call( self ) if @on_success 144: end
Invoked when extended data arrives over the channel. This simply delegates to the on_stderr callback, if registered, if the type is 1; otherwise it does nothing.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 167 167: def do_extended_data( channel, type, data ) 168: case type 169: when 1 170: @on_stderr.call( self, data ) if @on_stderr 171: end 172: end
Invoked when a channel request is received.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 181 181: def do_request( channel, type, want_reply, data ) 182: case type 183: when "exit-status" 184: @exit_status = data.read_long 185: end 186: end
Register the given block to be invoked when the process terminates normally. The block should accept two parameters: the process instance (self) and the exit status of the process.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 93 93: def on_exit( &block ) 94: @on_exit = block 95: end
Register the given block to be invoked when the command could not be started. The block should accept two parameters: the process instance (self) and a status string. (The status string is currently always nil, since SSH itself does not indicate why the program failed to start.)
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 66 66: def on_failure( &block ) 67: @on_failure = block 68: end
Register the given block to be invoked when data is recieved from the invoked command‘s stderr stream. The block should accept two parameters: the process instance (self) and the data string. Note that if the process sends large amounts of data, this method may be invoked multiple times, each time with a portion of the command‘s error output.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 86 86: def on_stderr( &block ) 87: @on_stderr = block 88: end
Register the given block to be invoked when data is recieved from the invoked command‘s stdout stream. The block should accept two parameters: the process instance (self) and the data string. Note that if the process sends large amounts of data, this method may be invoked multiple times, each time with a portion of the command‘s output.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 76 76: def on_stdout( &block ) 77: @on_stdout = block 78: end
Register the given block to be invoked when the command has been confirmed to have been successfully started. The block should accept a single parameter, the process instance that was created (self).
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 57 57: def on_success( &block ) 58: @on_success = block 59: end
Send the given data to the process, appending a newline. As with Kernel::puts, this will not append a newline if the string already has one. See write.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 106 106: def puts( data ) 107: @channel.send_data data.chomp + "\n" 108: end
Send the given data to the process. It will be sent via the process‘s stdin stream. This method returns immediately.
[ show source ]
# File lib/net/ssh/service/process/open.rb, line 99 99: def write( data ) 100: @channel.send_data data 101: end