Class: SSH
- Inherits:
-
Object
- Object
- SSH
- Defined in:
- lib/command/ssh/ssh.rb
Overview
Run SSH commands remotely on a server.
Instance Method Summary collapse
-
#initialize(username, host, password = nil) ⇒ SSH
constructor
A new instance of SSH.
-
#run(command) ⇒ Object
Run the SSH command on the server.
Constructor Details
#initialize(username, host, password = nil) ⇒ SSH
Returns a new instance of SSH.
5 6 7 8 9 |
# File 'lib/command/ssh/ssh.rb', line 5 def initialize(username, host, password = nil) @username = username @host = host @password = password end |
Instance Method Details
#run(command) ⇒ Object
Run the SSH command on the server
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/command/ssh/ssh.rb', line 12 def run(command) Net::SSH.start(@host, @username, password: @password) do |ssh| output = '' exit_code = nil ssh.open_channel do |channel| channel.exec(command) do |_ch, success| abort "FAILED: Couldn't execute command #{command}" unless success channel.on_data do |_ch, data| puts data output += data end channel.on_extended_data do |_ch, _type, data| puts data output += data end channel.on_request('exit-status') { |_ch, data| exit_code = data.read_long } end end ssh.loop return { output: output, result: exit_code <= 0 } end end |