daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Jan 14th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'net/ssh'
  2.  
  3. class Net::SSH::Connection::Session
  4.  
  5.   class Result < Struct.new(:stdout, :stderr, :exit_status, :exit_signal)
  6.     def success?
  7.       exit_status == 0
  8.     end
  9.   end
  10.  
  11.   def execute command
  12.     result = Result.new("", "", nil, nil)
  13.  
  14.     open_channel do |channel|
  15.       escaped_command = command.gsub(/'/,%{'"'"'})
  16.       channel.exec(%{bash -lc '#{escaped_command}'}) do |ch, success|
  17.         raise "Could not execute command: #{command.inspect}" unless success
  18.         channel.on_data{ |ch, data| result.stdout += data }
  19.         channel.on_extended_data{ |ch, type, data| result.stderr += data }
  20.         channel.on_request("exit-status"){ |ch, data| result.exit_status = data.read_long }
  21.         channel.on_request("exit-signal"){ |ch, data| result.exit_signal = data.read_long }
  22.       end
  23.     end
  24.  
  25.     loop
  26.  
  27.     result
  28.   end
  29.  
  30. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top