Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From <code>ri Net::SSH::start</code>:</p> <pre><code> -------------------------------------------------------- Net::SSH::start Net::SSH::start(host, user, options={}, &amp;block) {|connection| ...} ------------------------------------------------------------------------ The standard means of starting a new SSH connection. When used with a block, the connection will be closed when the block terminates, otherwise the connection will just be returned. The yielded (or returned) value will be an instance of Net::SSH::Connection::Session (q.v.). (See also Net::SSH::Connection::Channel and Net::SSH::Service::Forward.) Net::SSH.start("host", "user") do |ssh| ssh.exec! "cp /some/file /another/location" hostname = ssh.exec!("hostname") ssh.open_channel do |ch| ch.exec "sudo -p 'sudo password: ' ls" do |ch, success| abort "could not execute sudo ls" unless success ch.on_data do |ch, data| print data if data =~ /sudo password: / ch.send_data("password\n") end end end end ssh.loop end </code></pre> <p>So it looks like you can get more interactive by using <code>#open_channel</code></p> <p>Here's some example code:</p> <pre><code>user@server% cat echo.rb #! /usr/local/bin/ruby def putsf s puts s STDOUT.flush end putsf "hello" 5.times do putsf gets.chomp end putsf "goodbye" </code></pre> <p>And on your local machine:</p> <pre><code>user@local% cat client.rb #! /usr/local/bin/ruby require 'rubygems' require 'net/ssh' words = %w{ earn more sessions by sleaving } index = 0; Net::SSH.start('server', 'user') do |ssh| ssh.open_channel do |ch| ch.exec './echo.rb' do |ch, success| abort "could not execute ./echo.rb" unless success ch.on_data do |ch, data| p [:data, data] index %= words.size ch.send_data( words[index] + "\n" ) index += 1 end end end end user@local% ./client.rb [:data, "hello\n"] [:data, "earn\n"] [:data, "more\n"] [:data, "sessions\n"] [:data, "by\n"] [:data, "sleaving\n"] [:data, "goodbye\n"] </code></pre> <p>So you can interact with a running process this way.</p> <p>It's important that the running process flush its output before requesting input - otherwise, the program might hang as the channel may not have received the unflushed output.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload