Forum: Ruby on Rails CGI and NET::SSH in the same script problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33016269f72c5f03f95404e49fdc9d3d?d=identicon&s=25 Steve Dame (sdameba)
on 2008-10-20 22:48
I'm perplexed as to why I can run the following just fine from a command
line, but when I try to execute from a web  browser it crashes.

From the command line it prints out...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><HTML><BODY... Control
Panel</H3><P>cmd=>[echo 'THIS IS A TEST FROM THE REMOTE SHELL'] got
stdout:
THIS IS A TEST FROM THE REMOTE SHELL
cmd=>[echo 'THIS IS A TEST FROM THE REMOTE SHELL']
closing!</P></BODY></HTML>

Which means if this text is rendered in a web environment it should
print
THIS IS A TEST FROM THE REMOTE SHELL

Does anyone know why you can't call net/ssh from a CGI or how to debug
this in a browser environment?

----------------------------- ruby cgi script
---------------------------------
require 'cgi'
require 'rubygems'
require 'net/ssh'

def do_sync_cmd(  session, command )
  session.open_channel do |channel|
  channel.env("PATH", "/usr/pbs/bin:/acct/uzer/test/bin")
  channel.exec("#{command}") do |ch, success|
      abort @myString.concat("could not execute #{command}") unless
success

      channel.on_data do |ch, data|
        @myString.concat("cmd=>[#{command}] got stdout: \n#{data}")
        channel.send_data "something for stdin\n"
      end

      channel.on_extended_data do |ch, type, data|
        @myString.concat("cmd=>[#{command}] stderr: \n#{data}")
      end

      channel.on_close do |ch|
        @myString.concat("cmd=>[#{command}] closing!\n\n")
      end
    end
  end
  session.loop
end


@myString = "XXX"

Net::SSH.start('myserver.com', 'uzer') do |session|
  do_sync_cmd session, "cd /acct/uzer/test/bin && ls -l"
end

cgi = CGI.new('html4')
params = cgi.params

cgi.out {
   cgi.html {
      cgi.head { cgi.title {'Test Control Panel v0.1'}}
      cgi.body {
         cgi.h3 {'Test Control Panel'} +
         cgi.p  {"#{@myString}"}
      }
   }
}
This topic is locked and can not be replied to.