class Net::SSH::Connection::Session

def exec!(command, status: nil, &block)

the returned string has an exitstatus method to query its exit status

matches = ssh.exec!("grep something /some/files")

as a single string.
if no block is given, this will return all output (stdout and stderr)
Same as #exec, except this will block until the command finishes. Also,
def exec!(command, status: nil, &block)
  block_or_concat = block || Proc.new do |ch, type, data|
    ch[:result] ||= String.new
    ch[:result] << data
  end
  status ||= {}
  channel = exec(command, status: status, &block_or_concat)
  channel.wait
  channel[:result] ||= String.new unless block
  channel[:result] &&= channel[:result].force_encoding("UTF-8") unless block
  StringWithExitstatus.new(channel[:result], status[:exit_code]) if channel[:result]
end