How do I get the server name?

L

laredotornado

Hi,

I'm using Ruby 1.8. How do I get my server name? I'm running Apache
2 on Fedora Core 6 Linux. I tried "ENV["server_name"]", but got an
error.

- Dave
 
A

Andrei Maxim

Since ENV is a hash, why don't you open up irb and type

ENV.each { |key, value| puts "#{lkey} - #{value}" }

It's a bit quicker than expecting a reply on a mailing list.

Hi,

I'm using Ruby 1.8. How do I get my server name? I'm running Apache
2 on Fedora Core 6 Linux. I tried "ENV["server_name"]", but got an
error.

- Dave
 
J

Jano Svitok

No, it's not.
=> false

It quacks like a hash, so it must be a hash ;-)
Of course, it's not a real Hash instance, but you can work with it as
though it was a Hash.
ENV.each { |key, value| puts "#{lkey} - #{value}" }

There's a typo: not #{lkey}, byt #{key}

Jano
 
A

Andrei Maxim

It quacks like a hash, so it must be a hash ;-)
Of course, it's not a real Hash instance, but you can work with it as
though it was a Hash.

I was actually surprised to see that ENV.class returns Object. It
certainly acts like a Hash so...
There's a typo: not #{lkey}, byt #{key}

Right. Sorry about that :)
 
S

s.ross

Hi,

I'm using Ruby 1.8. How do I get my server name? I'm running Apache
2 on Fedora Core 6 Linux. I tried "ENV["server_name"]", but got an
error.

- Dave

`hostname`.chomp
 
L

laredotornado


Thanks all for your replies but there's something odd. I guess I'm
still confused about how to access values from this thing that is not
quite a hash. In irb, I did as Andrei suggested and thought I saw a
HOSTNAME key, but when I try and print out the value ("puts
ENV['HOSTNAME']"), I get nil. Why does it appear when I print out the
hash but not when I try and access the value individually?

Here's the irb action:

irb(main):068:0* ENV.each { |key, value| puts "#{key} - #{value}" }
USER - myusername
LOGNAME - myusername
HOME - /home/myusername
PATH - /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/myusername/
bin
MAIL - /var/spool/mail/myusername
SHELL - /bin/zsh
SSH_CLIENT - 67.190.94.163 61497 22
SSH_CONNECTION - 67.190.94.163 61497 192.168.1.99 22
SSH_TTY - /dev/pts/1
TERM - xterm
SHLVL - 1
PWD - /usr/local/apache2/htdocs/easyrx/app/controllers
OLDPWD - /usr/local/apache2/htdocs/easyrx/app
HOSTNAME - mydomain.com
HISTSIZE - 1000
INPUTRC - /etc/inputrc
LS_COLORS -
no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:eek:r=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
CVS_RSH - ssh
CVSROOT - /usr/local/cvs-repository
G_BROKEN_FILENAMES - 1
SSH_ASKPASS - /usr/libexec/openssh/gnome-ssh-askpass
KDEDIR - /usr
LANG - en_US.UTF-8
LESSOPEN - |/usr/bin/lesspipe.sh %s
QTDIR - /usr/lib/qt-3.3
QTINC - /usr/lib/qt-3.3/include
QTLIB - /usr/lib/qt-3.3/lib
JAVA_HOME - /usr/lib/jdk
CATALINA_HOME - /usr/local/tomcat
_ - /usr/local/bin/irb
LINES - 46
COLUMNS - 157
=> {"USER"=>"myusername", "LOGNAME"=>"myusername", "HOME"=>"/home/
myusername", "PATH"=>"/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/
home/myusername/bin", "MAIL"=>"/var/spool/mail/myusername", "SHELL"=>"/
bin/zsh", "SSH_CLIENT"=>"67.190.94.163 61497 22",
"SSH_CONNECTION"=>"67.190.94.163 61497 192.168.1.99 22", "SSH_TTY"=>"/
dev/pts/1", "TERM"=>"xterm", "SHLVL"=>"1", "PWD"=>"/usr/local/apache2/
htdocs/easyrx/app/controllers", "OLDPWD"=>"/usr/local/apache2/htdocs/
easyrx/app", "HOSTNAME"=>"mydomain.com", "HISTSIZE"=>"1000",
"INPUTRC"=>"/etc/inputrc",
"LS_COLORS"=>"no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:eek:r=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:",
"CVS_RSH"=>"ssh", "CVSROOT"=>"/usr/local/cvs-repository",
"G_BROKEN_FILENAMES"=>"1", "SSH_ASKPASS"=>"/usr/libexec/openssh/gnome-
ssh-askpass", "KDEDIR"=>"/usr", "LANG"=>"en_US.UTF-8", "LESSOPEN"=>"|/
usr/bin/lesspipe.sh %s", "QTDIR"=>"/usr/lib/qt-3.3", "QTINC"=>"/usr/
lib/qt-3.3/include", "QTLIB"=>"/usr/lib/qt-3.3/lib", "JAVA_HOME"=>"/
usr/lib/jdk", "CATALINA_HOME"=>"/usr/local/tomcat", "_"=>"/usr/local/
bin/irb", "LINES"=>"46", "COLUMNS"=>"157"}
 
R

Ron Fox

How about

require "socket"
....

host_name = Socket::gethostname


RF said:

Thanks all for your replies but there's something odd. I guess I'm
still confused about how to access values from this thing that is not
quite a hash. In irb, I did as Andrei suggested and thought I saw a
HOSTNAME key, but when I try and print out the value ("puts
ENV['HOSTNAME']"), I get nil. Why does it appear when I print out the
hash but not when I try and access the value individually?
 
7

7stud --

laredotornado said:
Thanks all for your replies but there's something odd. I guess I'm
still confused about how to access values from this thing that is not
quite a hash. In irb, I did as Andrei suggested and thought I saw a
HOSTNAME key, but when I try and print out the value ("puts
ENV['HOSTNAME']"), I get nil. Why does it appear when I print out the
hash but not when I try and access the value individually?

Here's the irb action:

irb(main):068:0* ENV.each { |key, value| puts "#{key} - #{value}" }
USER - myusername
LOGNAME - myusername
HOME - /home/myusername
PATH - /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/myusername/
bin
MAIL - /var/spool/mail/myusername
SHELL - /bin/zsh
SSH_CLIENT - 67.190.94.163 61497 22
SSH_CONNECTION - 67.190.94.163 61497 192.168.1.99 22
SSH_TTY - /dev/pts/1
TERM - xterm
SHLVL - 1
PWD - /usr/local/apache2/htdocs/easyrx/app/controllers
OLDPWD - /usr/local/apache2/htdocs/easyrx/app
HOSTNAME - mydomain.com
HISTSIZE - 1000
INPUTRC - /etc/inputrc
LS_COLORS -

Why did you think leaving out the:

puts ENV['HOSTNAME']

as the next statement would help prove your point?
 
B

Brian Candler

I'm using Ruby 1.8. How do I get my server name? I'm running Apache
2 on Fedora Core 6 Linux. I tried "ENV["server_name"]", but got an
error.

You shouldn't get an error - it should give nil for unknown key.

If you're using erb templates, try something like

<%= ENV.inspect %>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top