Sha256: 4fc657cb6b1039b785a50558b7d010d71230063843e125c37f14c3a2585c8b4a

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

To run multiple processes in parallel, you can access the channel API directly, setting up multiple channels and callbacks in order to process the output from the channel.

Suppose, for example, that you wanted to run multiple "tail" commands on various logs on the remote machine, combining them all into the output on the client. Something like the following would suffice:

{{{lang=ruby,number=true,caption=Running "tail" on multiple remote files
def do_tail( session, file )
  session.open_channel do |channel|
    channel.on_data do |ch, data|
      puts "[#{file}] -> #{data}"
    end
    channel.exec "tail -f #{file}"
  end
end

Net::SSH.start( 'host' ) do |session|
  do_tail session, "/var/log/messages"
  do_tail session, "/var/log/XFree86.0.log"
  do_tail session, "/var/log/tomcat/catalina.log"
  do_tail session, "/var/log/mysql/mysql.err"
  session.loop
end
}}}

As you can see, four different logs are tailed on four separate channels. Each channel registers an @on_data@ callback (which simply displays the data it recieves, together with the name of the log file it came from). The @exec@ method of the channel is then invoked, which simply sends the request to execute the process to the server, and then returns.

The @loop@ method then blocks while packets and processed and callbacks are invoked, completing the program.

This approach works fine for processing data coming from the server, and with a little work and coordination can work well for sending data _to_ the server as well, by calling the @send_data@ method of the channel at the appropriate times. However, it requires a bit of forethought, since you have to come up with a simple state machine to manage most interactive sessions, and many times that's more effort than it is worth.

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
net-ssh-1.0.1 doc/manual/parts/0016.txt
net-ssh-1.0.10 doc/manual/parts/0016.txt
net-ssh-1.0.9 doc/manual/parts/0016.txt
net-ssh-1.0.5 doc/manual/parts/0016.txt
net-ssh-1.0.7 doc/manual/parts/0016.txt
net-ssh-1.0.8 doc/manual/parts/0016.txt
net-ssh-1.0.4 doc/manual/parts/0016.txt
net-ssh-1.0.2 doc/manual/parts/0016.txt
net-ssh-1.0.3 doc/manual/parts/0016.txt
net-ssh-1.0.6 doc/manual/parts/0016.txt
net-ssh-1.1.1 doc/manual/parts/0016.txt
net-ssh-1.1.0 doc/manual/parts/0016.txt
net-ssh-1.1.2 doc/manual/parts/0016.txt
net-ssh-1.1.3 doc/manual/parts/0016.txt
net-ssh-1.1.4 doc/manual/parts/0016.txt