1

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"

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.