Reputation: 15093
I wanted to run a remote command with ruby
's net::ssh
and was hoping to print the output stdout
but i don't see anything printed at the below code at channel.on_data
see test code:
Net::SSH.start('testhost', "root", :timeout => 10) do |ssh|
ssh.open_channel do |channel|
channel.exec('ls') do |_, success|
unless success
puts "NOT SUCCEESS:! "
end
channel.on_data do |_, data|
puts "DATAAAAAA:! " # ======> Why am i not getting to here?? <=======
end
channel.on_extended_data do |_, _, data|
puts "EXTENDED!!!"
end
channel.on_request("exit-status") do |_, data|
puts "EXIT!!!! "
end
channel.on_close do |ch|
puts "channel is closing!"
end
end
end
end
and the output is:
channel is closing!
why don't i get into the block on_data
? I want to grab the stdout
.
note that i know the client code is able to ssh
to the remote server
because when I asked the command to be ls > ls.log
I saw that ls.log
on target host.
Upvotes: 0
Views: 622
Reputation: 10536
Note that opening a channel is asynchronous, so you have to wait for the channel to do anything meaningful, otherwise you are closing the connection too soon.
Try this:
Net::SSH.start('test', "root", :timeout => 10) do |ssh|
ch = ssh.open_channel do |channel|
channel.exec('ls') do |_, success|
unless success
puts "Error"
end
channel.on_data do |_, data|
puts data
end
channel.on_extended_data do |_, _, data|
puts data
end
channel.on_request("exit-status") do |_, data|
puts "Exit"
end
channel.on_close do |ch|
puts "Closing!"
end
end
end
ch.wait
end
Upvotes: 2