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.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 37 37: def initialize( connection, log, pty_opts ) 38: @connection = connection 39: @log = log 40: 41: @pty_opts = pty_opts 42: 43: @stdout = "" 44: @stderr = "" 45: 46: @state = :opening 47: @connection.open_channel( "session", &method( :on_confirm ) ) 48: 49: @connection.loop { @state != :open && @state != :closed } 50: raise "could not open shell" if @state != :open 51: 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.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 113 113: def method_missing( sym, *args ) 114: cmd = sym.to_s 115: cmd << " " << args.join( " " ) unless args.empty? 116: send_data cmd + "\n" 117: end
Returns true if the shell is open.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 54 54: def open? 55: @state == :open 56: end
Sends the given data to the shell on the shell‘s stdin stream.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 97 97: def send_data( data ) 98: raise "channel not open" unless @state == :open 99: @channel.send_data data 100: end
Sends the given data to the shell on the stream indicated by the type parameter.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 104 104: def send_extended_data( type, data ) 105: raise "channel not open" unless @state == :open 106: @channel.send_extended_data type, data 107: end
Return the stderr output (if any) that the shell has generated since the last time this method was invoked.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 79 79: def stderr 80: string, @stderr = @stderr, "" 81: string 82: 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.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 87 87: def stderr? 88: exists = @stderr.length > 0 89: unless exists 90: consume_connection 91: exists = @stderr.length > 0 92: end 93: exists 94: end
Return the stdout output (if any) that the shell has generated since the last time this method was invoked.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 60 60: def stdout 61: string, @stdout = @stdout, "" 62: string 63: 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.
[ show source ]
# File lib/net/ssh/service/shell/shell.rb, line 68 68: def stdout? 69: exists = @stdout.length > 0 70: unless exists 71: consume_connection 72: exists = @stdout.length > 0 73: end 74: exists 75: end