# CGI and NET::SSH in the same script problems

**URL:** <https://www.ruby-forum.com/t/cgi-and-net-ssh-in-the-same-script-problems/151393>\
**Category:** Rails\
**Created:** [October 20, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/cgi-and-net-ssh-in-the-same-script-problems/151393 "2008-10-20T22:48:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [October 20, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/cgi-and-net-ssh-in-the-same-script-problems/151393/1 "2008-10-20T22:48:41Z")

</div>

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…

### FlyBOB Control

Panel

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!

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](http://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}”}  
}  
}  
}
