Class | Net::SSH::Service::Shell::Shell |
In: |
lib/net/ssh/service/shell/shell.rb
|
Parent: | Object |
A service class for interacting with a user’s shell on a remote machine. The shell may be interacted with either with or without a pty.
Create a new shell over the given connection. The pty_opts parameter must be either a Hash of the allowed values for the Net::SSH::Connection::Channel#request_pty method, or a boolean value (indicating whether a pty should be allocated or not). This will block until the shell is open and ready to receive input.
# File lib/net/ssh/service/shell/shell.rb, line 34 34: def initialize( connection, log, pty_opts ) 35: @connection = connection 36: @log = log 37: 38: @pty_opts = pty_opts 39: 40: @stdout = "" 41: @stderr = "" 42: 43: @state = :opening 44: @connection.open_channel( "session", &method( :on_confirm ) ) 45: 46: @connection.loop { @state != :open && @state != :closed } 47: raise "could not open shell" if @state != :open 48: end
Reinterprets method invocations as requests to send data to the shell. The method name and the arguments are concatenated together with spaces and a newline appended. The resulting string is sent to the shell via send_data.
# File lib/net/ssh/service/shell/shell.rb, line 110 110: def method_missing( sym, *args ) 111: cmd = sym.to_s 112: cmd << " " << args.join( " " ) unless args.empty? 113: send_data cmd + "\n" 114: end
Returns true if the shell is open.
# File lib/net/ssh/service/shell/shell.rb, line 51 51: def open? 52: @state == :open 53: end
Sends the given data to the shell on the shell’s stdin stream.
# File lib/net/ssh/service/shell/shell.rb, line 94 94: def send_data( data ) 95: raise "channel not open" unless @state == :open 96: @channel.send_data data 97: end
Sends the given data to the shell on the stream indicated by the type parameter.
# File lib/net/ssh/service/shell/shell.rb, line 101 101: def send_extended_data( type, data ) 102: raise "channel not open" unless @state == :open 103: @channel.send_extended_data type, data 104: end
Return the stderr output (if any) that the shell has generated since the last time this method was invoked.
# File lib/net/ssh/service/shell/shell.rb, line 76 76: def stderr 77: string, @stderr = @stderr, "" 78: string 79: end
Returns true if there is any data from the shell on stderr, consuming input on the connection in a non-blocking manner to make sure that any available data is considered.
# File lib/net/ssh/service/shell/shell.rb, line 84 84: def stderr? 85: exists = @stderr.length > 0 86: unless exists 87: consume_connection 88: exists = @stderr.length > 0 89: end 90: exists 91: end
Return the stdout output (if any) that the shell has generated since the last time this method was invoked.
# File lib/net/ssh/service/shell/shell.rb, line 57 57: def stdout 58: string, @stdout = @stdout, "" 59: string 60: end
Returns true if there is any data from the shell on stdout, consuming input on the connection in a non-blocking manner to make sure that any available data is considered.
# File lib/net/ssh/service/shell/shell.rb, line 65 65: def stdout? 66: exists = @stdout.length > 0 67: unless exists 68: consume_connection 69: exists = @stdout.length > 0 70: end 71: exists 72: end