Net :: SSH :: Multi using session.exec, how do you get the result right away? Ruby

So, I'm trying to use Net :: SSH :: Multi to log in to multiple computers using SSH and then execute shell commands on remote machines with session.exec ("some_command").

Code:

#!/usr/bin/ruby require 'rubygems' require 'net/ssh' require 'net/ssh/multi' Net::SSH::Multi.start do |session| # Connect to remote machines ### Change this!!### session.use ' user@server ' loop = 1 while loop == 1 printf(">> ") command = gets.chomp if command == "quit" then loop = 0 else session.exec(command)do |ch, stream, data| puts "[#{ch[:host]} : #{stream}] #{data}" end end end end 

The problem I'm currently facing is when I type the command in an interactive prompt, "session.exec" does not return a result. I quit the program, I was wondering if anyone ran into this problem and can tell me how I can solve this problem?

+4
source share
3 answers

Adding session.loop after session.exec allows the program to wait for output.

For instance:

 session.exec(command)do |ch, stream, data| puts "[#{ch[:host]} : #{stream}] #{data}" end session.loop # Or session.wait also does the same job. 
+4
source

Take a look here . The exec method apparently gives the result in the supplied block.

Example from the documentation:

 session.exec("command") do |ch, stream, data| puts "[#{ch[:host]} : #{stream}] #{data}" end 

Disclaimer: I have not experienced this myself. It may work or not. Let us know when it works!

0
source

Remove the while loop and call session.loop after exec is called. Something like that:

 Net::SSH::Multi.start do |session| # Connect to remote machines ### Change this!!### session.use ' user@server ' session.exec(command)do |ch, stream, data| puts "[#{ch[:host]} : #{stream}] #{data}" end # Tell Net::SSH to wait for output from the SSH server session.loop end 
0
source

All Articles