Tony D
Tony D

Reputation: 49

Getting output from Ruby Net::SSH sessions

I've got a larger script that basically uses the same type of code below. The script runs and functions but I don't get output to the screen. How do I get the output of the script that is being executed remotely to show up on the screen I'm running the ruby script from?

#!/usr/bin/ruby
#
require 'rubygems'
require 'net/ssh'
require 'pty'

if ENV['USER'] == 'root'
  raise "You can't run this as root"
end

Net::SSH.start(server01, testuser) do |ssh|
  ssh.open_channel do |channel|
     channel.on_request "exit-status" do |channel, data|
     $exit_status = data.read_long
  end
channel.on_data do |channel, data|
 data
end
  channel.request_pty do |channel, data|
  channel.exec("sudo -s")
  channel.send_data("/tmp/scripts/test.sh\n") 
  channel.send_data("exit\n")
  end
 end
end
puts "DONE"

Upvotes: 1

Views: 898

Answers (1)

jtzero
jtzero

Reputation: 2254

change

channel.on_data do |channel, data|
 data
end

to

channel.on_data do |channel, data|
 puts data
end

the data mentioned is the response from the server

and add

channel.send_channel_request 'shell' do |ch, success|
  if success
    puts 'user shell started successfully'
  else
    puts 'could not start user shell'
  end
end

Upvotes: 2

Related Questions