# Cgi scripts don't know hostname

**URL:** <https://www.ruby-forum.com/t/cgi-scripts-dont-know-hostname/115035>\
**Category:** Ruby\
**Created:** [September 19, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/cgi-scripts-dont-know-hostname/115035 "2007-09-19T23:42:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [September 19, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/cgi-scripts-dont-know-hostname/115035/1 "2007-09-19T23:42:46Z")

</div>

Forgive me if this is a dumb question, but it’s got me stumped.

I’m running a WEBrick server and I’ve mounted HTTPServlet::FileHandler  
as follows:

s.mount("/cgi-bin",HTTPServlet::FileHandler,  
cgi\_dir,{:FancyIndexing=\>true})

However, the scripts in the cgi-bin directory can’t find the hostname  
when they’re run by the web server. E.g., the following script

#!/usr/bin/ruby -w

s = %{  
  
  
Hi #{ENV[‘HOSTNAME’]} Bye  
  
  
}  
print s

prints the html with the machine name embedded in it when I run it from  
the command line, but only “Hi Bye” when I look at it in the browser.  
What’s up with that?

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [September 21, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/cgi-scripts-dont-know-hostname/115035/2 "2007-09-21T17:15:44Z")

</div>

It turns out that the cgi scripts run this way have an empty or at least  
incomplete ENV. I worked around it by marshaling the server’s ENV and  
having  
the cgi’s read it at startup. Not a great solution, but it works.

Mark P. wrote:

> Forgive me if this is a dumb question, but it’s got me stumped.
> 
> I’m running a WEBrick server and I’ve mounted HTTPServlet::FileHandler  
> as follows:
> 
> s.mount("/cgi-bin",HTTPServlet::FileHandler,  
> cgi\_dir,{:FancyIndexing=\>true})
> 
> However, the scripts in the cgi-bin directory can’t find the hostname  
> when they’re run by the web server. E.g., the following script
> 
> #!/usr/bin/ruby -w
> 
> s = %{  
>   
>   
> Hi #{ENV[‘HOSTNAME’]} Bye  
>   
>   
> }  
> print s
> 
> prints the html with the machine name embedded in it when I run it from  
> the command line, but only “Hi Bye” when I look at it in the browser.  
> What’s up with that?
